3

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?

Developer
  • 8,390
  • 41
  • 129
  • 238
user902080
  • 163
  • 3
  • 5
  • 10

2 Answers2

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
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