2

I'm using the Play Framework and taking a text from a textarea and I want to split it into an array of the words, spaces and newlines that was entered.

Hello World How

Are You

Would be something like

a[0] = "Hello";
a[1] = " ";
a[2] = "World";
a[3] = " ";
a[4] = "How";
a[5] = "\n";
a[6] = "Are";
a[7] = " ";
a[8] = "You";

If there is a simple regex way or something similar I would love to hear about it?

Community
  • 1
  • 1
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198

3 Answers3

6

Try this code:

String str = "Hello World How\nAre You";
String[] inputs = str.split("(?!^)\\b");
for (int i=0; i<inputs.length; i++) {
   System.out.println("a[" + i + "] = \"" + inputs[i] + '"');
}

OUTPUT:
a[0] = "Hello"
a[1] = " "
a[2] = "World"
a[3] = " "
a[4] = "How"
a[5] = "
"
a[6] = "Are"
a[7] = " "
a[8] = "You"
anubhava
  • 761,203
  • 64
  • 569
  • 643
3
st = new java.util.StringTokenizer (text, "[ \t\n]", true)
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • 1
    @Ólafur Waage: *StringTokenizer is a legacy class that is retained for compatibility reasons although its use is **discouraged** in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.* From Javadocs: http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html – anubhava Apr 13 '11 at 11:56
0

StringTokenizer would work.

Val
  • 173
  • 2
  • 10