如何划分一个句子包含中间的逗号到部分Java?

时间:2012-06-23 05:28:51

标签: java string text

如何将"He and his brother, are playing football."之类的句子分成几个部分,如"He""He and""and his""his brother""brother, ",{ {1}},", are""brother playing""playing football""football."。是否可以通过使用Java来实现?

"."

这是我所做的代码问题是“,”句子内部必须与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)); 之类的单词分开,因为这只"brother,"只能起作用。有解决方案吗

1 个答案:

答案 0 :(得分: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。它也将分裂出现一个逗号。