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?
Asked
Active
Viewed 260 times
-2
Ankur Shanbhag
- 7,746
- 2
- 28
- 38
user1433755
- 77
- 1
- 3
- 10
-
So many related answers...The `|` character is called the `pipe`. – Sotirios Delimanolis Dec 11 '13 at 17:48
2 Answers
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