-2

I want to split a string by | in java but as split function in java accepts regular expressions and | means or operator, I am not sure what I should provide the split function with?

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
user1433755
  • 77
  • 1
  • 3
  • 10

2 Answers2

1

Provide escape sequence before |, something like this : \\|

String str = "abc|pqr";
String[] split=str.split("\\|");
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
0

You can escape the String with Pattern.quote(String s); before passing it to the split() method

Gijs Overvliet
  • 2,643
  • 3
  • 28
  • 35