Why does String.split need pipe delimiter to be escaped?

JavaRegexString

Java Problem Overview


I am trying to parse a file that has each line with pipe delimited values. It did not work correctly when I did not escape the pipe delimiter in split method, but it worked correctly after I escaped the pipe as below.

private ArrayList<String> parseLine(String line) {
	ArrayList<String> list = new ArrayList<String>();
	String[] list_str = line.split("\\|"); // note the escape "\\" here
	System.out.println(list_str.length);
	System.out.println(line);
	for(String s:list_str) {
		list.add(s);
		System.out.print(s+ "|");
	}
	return list;
}

Can someone please explain why the pipe character needs to be escaped for the split() method?

Java Solutions


Solution 1 - Java

String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean.

Solution 2 - Java

Because the syntax for that parameter to split is a regular expression, where in the '|' has a special meaning of OR, and a '\|' means a literal '|' so the string "\\|" means the regular expression '\|' which means match exactly the character '|'.

Solution 3 - Java

You can simply do this:

String[] arrayString = yourString.split("\\|");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstarthisView Question on Stackoverflow
Solution 1 - JavaLouis WassermanView Answer on Stackoverflow
Solution 2 - JavadlamblinView Answer on Stackoverflow
Solution 3 - JavaRavinathView Answer on Stackoverflow