When I split the string "1|2|3|4" using the String.split("|") I get 8 elements in the array instead of 4. If I use "\\|" the result is proper. I am guessing this has something todo with regular expressions. Can anybody explain this?
When I split the string 1|2|3|4 using the String.split(|) I get 8 elements in
Share
You’re right.
|is a special character for alternation. The regular expression|means “an empty string or an empty string”. So it will split around all empty strings, resulting 1 element for each character in the string. Escaping it\|make it a normal character.