-1

I pull string from textarea by .gettext() and it have a blankline.

test1111  

test222

I need to split this String and keep into array, then array[0]=test1111 array[1]=test222

How can I do?

Pshemo
  • 113,402
  • 22
  • 170
  • 242
Slatan-ko
  • 248
  • 1
  • 6
  • 18

4 Answers4

1

Something like this should work:

String[] lines = text.split("\\s*\\n+\\s*");

Or better if you're using Java 8 (per Pshemo), use "\\R+"

This will skip over multiple blank lines, or lines filled with white space, and should trim leading and ending whitespace as well.

Pshemo
  • 113,402
  • 22
  • 170
  • 242
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
0
String[] lines = jtextFieldName_you_used.getText().split("\\n");

It is to store textarea elements to array.Hope you find this helpful.

SmashCode
  • 713
  • 1
  • 8
  • 12
0

Try this.

String str = abc.getText();
for (String retval: str.split(" ")){ System.out.println(retval); }
mo sean
  • 2,332
  • 1
  • 19
  • 33
0

If i understood your task right:

final String[] lines = data.split("\\n");
final String results[] = new String[lines.length];
int offset = 0;

for (String line : lines) {
   results[offset] = line.split("\\s")[1];
   offset++;
}

results:

test1111
blankline
test222

p.s: without data checks

Evgeny Lebedev
  • 1,233
  • 1
  • 18
  • 29