16

每个人。

我有一个这样的字符串

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+");

问题是它包含“那个”,而不是我想要的“那个”。请教我如何解决它。谢谢

4

3 回答 3

38

你可以做

String[] dic = message.split("\\W+");

\\W表示不是字母数字字符。

于 2012-05-12T11:18:07.480 回答
7

您可以使用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());
 }
于 2012-05-12T11:24:15.870 回答
3

使用番石榴

// 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);
于 2012-05-12T11:22:17.837 回答