大家。
我有一个像这样的字符串
String message = "This is the new message or something like that, OK";
我想把它拆分成数组
String[] dic = {"this", "is", "the", "new", "message", "or", "something", "like", "that", "OK"};
我用过
message = message.split("\\s+");
问题在于它包含“那个”,而不是“我想要的那样”。请教我如何解决它。感谢
答案 0 :(得分:29)
你可以做到
String[] dic = message.split("\\W+");
\\W
表示不是字母数字字符。
答案 1 :(得分:3)
使用Guava:
// define splitter as a constant
private static final Splitter SPLITTER =
Splitter.on(CharMatcher.WHITESPACE.or(CharMatcher.is(','))
.trimResults()
.omitEmptyStrings();
// ...
// and now use it in your code
String[] str = Iterables.toArray(SPLITTER.split(yourString), String.class);
答案 2 :(得分:3)
您可以使用StringTokenizer
String message = "This is the new message or something like that, OK";
String delim = " \n\r\t,.;"; //insert here all delimitators
StringTokenizer st = new StringTokenizer(message,delim);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}