Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - String splits by every character

Tags:

java

split

When I try to split a string with a delimiter "|", it seems to split every single character.

This is my line which is causing the problem:

String out = myString.split("|");
like image 282
Yharoomanner Avatar asked Oct 04 '14 11:10

Yharoomanner


1 Answers

In regex, | is a reserved character used for alternation. You need to escape it:

String out = string.split("\\|");

Note that we used two backslashes. This is because the first one escapes the second one in the Java string, so the string passed to the regex engine is \|.

like image 89
Farsuer Avatar answered Sep 22 '22 17:09

Farsuer