16

everyone.

I have a string like this

String message = "This is the new message or something like that, OK";

And I want to split it into array

String[] dic = {"this", "is", "the", "new", "message", "or", "something", "like", "that", "OK"};

I used

message = message.split("\\s+");

The problem was that it contained "that," not "that" like I want. Please teach my how to solve it. Thanks

Gia Duong Duc Minh
  • 1,319
  • 5
  • 15
  • 30
  • possible duplicate of [Splitting strings through regular expressions by punctuation and whitespace etc in java](http://stackoverflow.com/questions/7384791/splitting-strings-through-regular-expressions-by-punctuation-and-whitespace-etc) – assylias May 12 '12 at 11:20

3 Answers3

40

You can do

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

The \\W means not an alphanumeric character.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
7

You can use 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());
 }
Alexandru Chirila
  • 2,274
  • 5
  • 29
  • 40
3

Use 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);
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588