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,228
  • 37
  • 122
  • 230
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,322
  • 16
  • 114
  • 93
Dennis Laumen
  • 3,053
  • 2
  • 20
  • 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,173
  • 9
  • 101
  • 148