I have the following line:
String str = "\nHERE\n\nTHERE\n\nEVERYWHERE\n\n";
If you just type this, it will be output as follows (of course, \n will not be "literally" printed):
\n HERE\n \n THERE\n \n EVERYWHERE\n \n \n
When I call the split("\n") method split("\n") , I want all lines between newlines ( \n ) to even empty lines at the end.
For example, if I do this today:
String strArray[] = str.split("\n"); System.out.println("strArray.length - " + strArray.length); for(int i = 0; i < strArray.length; i++) System.out.println("strArray[" + i + "] - \"" + strArray[i] + "\"");
I want it to print like this (Output A):
strArray.length - 8 strArray[0] - "" strArray[1] - "HERE" strArray[2] - "" strArray[3] - "THERE" strArray[4] - "" strArray[5] - "EVERYWHERE" strArray[6] - "" strArray[7] - ""
It is currently printed as follows (output B), and any trailing blank lines are skipped:
strArray.length - 6 strArray[0] - "" strArray[1] - "HERE" strArray[2] - "" strArray[3] - "THERE" strArray[4] - "" strArray[5] - "EVERYWHERE"
How do I make the split() method include blank lines, as in Output A? Of course, I could write a multi-line code fragment, but I wanted to know before I wasted time on its implementation if there was a simple way or two additional lines of code that could help me. Thank!
java string split
Rob Avery IV Dec 18 '12 at 19:01 2012-12-18 19:01
source share