java Java如何将一个句子分成几部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11095096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to divide a sentence into parts Java?
提问by cchua
How can I divide a sentence like "He and his brother playing football."
into few part like "He and"
, "and his"
, "his brother"
, "brother playing"
and "playing football"
. Is it possible to do that by using Java?
我怎样才能把一个句子 like"He and his brother playing football."
分成几个部分,比如"He and"
, "and his"
, "his brother"
,"brother playing"
和"playing football"
。是否可以通过使用 Java 来做到这一点?
回答by st0le
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]);
回答by Kumar Vivek Mitra
You can do it using BreakIterator classand its static method getSentenceInstance().It Returns a new BreakIterator instance for sentence breaks for the default locale
.
您可以使用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
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....
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();
}
回答by amicngh
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]);
}
回答by iracigt
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.
可能是也可能不是你所追求的。