1

我怎样才能将一个句子分成"He and his brother, are playing football."几个部分"He",如"He and",、、、、、、、、、"and his"和。是否可以通过使用Java来做到这一点?"his brother""brother, "", are""brother playing""playing football""football.""."

String[] words = "He and his brother , are playing football .".split("\\s+");
System.out.println(words[0]);
    for (int i = 0, l = words.length; i + 1 < l; i++){
    System.out.println(words[i] + " " + words[i + 1]);  
}
String s = words[words.length-1];       
System.out.println(s.charAt(s.length() - 1));

这是我做的代码问题是“,”里面的句子必须与单词 like "brother,"must put 分开,因为这"brother ,"只会起作用。有什么解决办法吗?

4

1 回答 1

0

改变这个:

String[] words = "He and his brother , are playing football .".split("\\s+");

至:

String[] words = "He and his brother , are playing football .".split("[\\s+,]");

这将处理,,也就是说,您不会得到brother,,而是只会得到brother。它也会在出现逗号时分裂。

于 2012-06-23T06:06:20.903 回答