I have a string that came from an array called fld[1].tostring
. When i try and split this string which returns "|R1|R2|R3"
on the | it splits it into each character. what am i doing wrong?
Asked
Active
Viewed 2,945 times
3

Developer
- 8,390
- 41
- 129
- 238

user902080
- 163
- 3
- 5
- 10
2 Answers
10
The split method accepts regular expressions. The pipe character is used to denote a logical or in Java regular expressions. Escape the character with a backslash to split on it.
For example:
String s = "|R1|R2|R3";
String[] a = s.split("\\|");

Konstantin Burov
- 68,980
- 16
- 115
- 93

Dennis Laumen
- 3,163
- 2
- 21
- 24
-
Awesome! thank you very very much! i needed a double \\ but you got me there! – user902080 Aug 24 '11 at 09:25
-
Sorry, my bad! I was a little bit too quick on the trigger I guess ;). – Dennis Laumen Aug 24 '11 at 09:28
-
|R1||R3|| getting fail in this case – Sandeep P Jun 04 '14 at 12:55
2
Vertical bar "|"
is special character. and String.split()
need a regualar expression.
try escaping it and treating it as special char:
fld[1].split("\\|");

Adil Soomro
- 37,609
- 9
- 103
- 153