如何将"He and his brother playing football."
之类的句子分成"He and"
,"and his"
,"his brother"
,"brother playing"
和"playing football"
等几个部分。是否可以通过使用Java来实现?
答案 0 :(得分:7)
假设“单词”总是由单个空格分隔。使用String.split()
String[] words = "He and his brother playing football.".split("\\s+");
for (int i = 0, l = words.length; i + 1 < l; i++)
System.out.println(words[i] + " " + words[i + 1]);
答案 1 :(得分:3)
您可以使用 BreakIterator类及其静态方法getSentenceInstance()来实现。
它Returns a new BreakIterator instance for sentence breaks for the default locale
。
You can also use getWordInstance(), getLineInstance().. to break words, line...etc
例如:
BreakIterator boundary = BreakIterator.getSentenceInstance();
boundary.setText("Your_Sentence");
int start = boundary.first();
int end = boundary.next();
Iterate over it... to get the Sentences....
有关详细信息,请查看此链接:
http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html
已编辑的答案:This is a working code
String sent = "My name is vivek. I work in TaxSmart";
BreakIterator bi = BreakIterator.getSentenceInstance();
bi.setText(sent);
int index = 0;
while (bi.next() != BreakIterator.DONE) {
String sentence = sent.substring(index, bi.current());
System.out.println("Sentence: " + sentence);
index = bi.current();
}
答案 2 :(得分:2)
String str="He and his brother playing football";
String [] strArray=str.split(" ");
for(int i=0;i<strArray.length-1 ;i++)
{
System.out.println(strArray[i]+" "+strArray[i+1]);
}
答案 3 :(得分:0)
使用StringTokenizer按空格或其他字符分隔。
import java.util.StringTokenizer;
public class Test {
private static String[] tokenize(String str) {
StringTokenizer tokenizer = new StringTokenizer(str);
String[] arr = new String[tokenizer.countTokens()];
int i = 0;
while (tokenizer.hasMoreTokens()) {
arr[i++] = tokenizer.nextToken();
}
return arr;
}
public static void main(String[] args) {
String[] strs = tokenize("Sandy sells seashells by the sea shore.");
for (String s : strs)
System.out.println(s);
}
}
应打印出来:
距
销售
海贝
通过
海
岸。
可能或可能不是你想要的。