[英]How to divide a sentence into parts Java?
How can I divide a sentence like "He and his brother playing football."
我怎么能分开一句话,比如
"He and his brother playing football."
into few part like "He and"
, "and his"
, "his brother"
, "brother playing"
and "playing football"
. 分成几个部分,如
"He and"
, "and his"
, "his brother"
, "brother playing"
和"playing football"
。 Is it possible to do that by using Java? 是否可以通过使用Java来实现?
Assuming the "words" are always separated by a single space. 假设“单词”总是由单个空格分隔。 Use
String.split()
使用
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]);
You can do it using BreakIterator class and its static method getSentenceInstance(). 您可以使用BreakIterator类及其静态方法getSentenceInstance()来完成此操作。 It
Returns a new BreakIterator instance for sentence breaks for the default locale
. 它
Returns a new BreakIterator instance for sentence breaks for the default locale
。
You can also use getWordInstance(), getLineInstance().. to break words, line...etc
eg: 例如:
BreakIterator boundary = BreakIterator.getSentenceInstance();
boundary.setText("Your_Sentence");
int start = boundary.first();
int end = boundary.next();
Iterate over it... to get the Sentences....
For more detail look at this link: 有关更多详细信息,请查看此链接:
http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html http://docs.oracle.com/javase/6/docs/api/java/text/BreakIterator.html
Edited Answer : This is a working code
编辑答案 :
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();
}
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]);
}
Use a StringTokenizer to separate by spaces or other characters. 使用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);
}
}
Should print out: 应打印出来:
Sandy 沙
sells 塞尔斯
seashells 贝壳
by 通过
the 该
sea 海
shore. 岸。
May or may not be what you're after. 可能是也可能不是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.