Splitting a Java string into "|"

I am trying to parse some data using Java, which is separated by the character '|' sequence. The following is sample data.

String s = "111206|00:00:00|2|64104|58041";
String [] temp = s.split("|");
for(String p: temp)
System.out.println(p);

But instead of dividing by '|' It separates each character separately. Here is the result I get for the above code.

 1
 1
 1
 2
 0
 6
 |
 0
 0
 :
 0
 0
 :
 0
 0
 |
 2
 |
 6
 4
 1
 0
 4
 |
 5
 8
 0
 4
 1

I found a twist by replacing '|' by ',' in the line, but the patch of code will be run many times, and I want to optimize it.

 String s = "111206|00:00:00|2|64104|58041";
 s = s.replace('|', ',');

I just want to know what is the problem with '|' ??

+5
source share
1 answer

You should use:

String [] temp = s.split("\\|");

, split , | - . "". , '' or '', ''. .

, \ , \ escape- Java . Java "\|", "|".

+11

All Articles