Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a String with the pipe symbol as delimiter

Tags:

java

string

split

Why is it that in the following, the output is [] and not [1]?

String input="1|2|3";
String[] values= input.split("|");
System.out.println("[" + values[0] + "]");
// Output: []

However, if we change the separator, the output is [1].

String input="1;2;3";
String[] values= input.split(";");
System.out.println("[" + values[0] + "]");
// Output: [1]
like image 758
Andreu Alcon Avatar asked Mar 05 '13 14:03

Andreu Alcon


People also ask

How do you split a string delimiter?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is delimiter split?

In Java, delimiters are the characters that split (separate) the string into tokens. Java allows us to define any characters as a delimiter. There are many string split methods provides by Java that uses whitespace character as a delimiter. The whitespace delimiter is the default delimiter in Java.

How do you break a string with special characters?

To split a string by special characters, call the split() method on the string, passing it a regular expression that matches any of the special characters as a parameter. The method will split the string on each occurrence of a special character and return an array containing the results.

How do you escape a pipe character in Java?

use \\| instead of | to escape it.


5 Answers

Try to escape that character:

String input="1|2|3";
String[] values= input.split("\\|");
System.out.println("[" + values[0] + "]");
like image 137
user Avatar answered Sep 21 '22 16:09

user


Because the | has special meaning in regular expressions. You need to escape it like this: \| and in Java you also have to escape the backslash as well so you end up with \\|

The pipe character is a disjunction operator which means that it tells the regular expression engine to choose either pattern on the left and right of it. In your case those where empty strings which match anything.

like image 23
Mike Dinescu Avatar answered Sep 20 '22 16:09

Mike Dinescu


The split method receives a regex as a parameter. The pipe is a reserved character with its own purpose (it means or).

You can either escape it ("\\|") or, if you're in Java 1.5+ you can use Pattern.quote("|") like this:

input.split(Pattern.quote("|"));
like image 42
Fritz Avatar answered Sep 20 '22 16:09

Fritz


Try using \\| instead of | when you split as you need to escape it.

So your code would change to:

String input="1|2|3";
String[] values= input.split("\\|");
System.out.println("[" + values[0] + "]");
like image 43
aa8y Avatar answered Sep 17 '22 16:09

aa8y


you have to escape the character '|' properly

String input="1|2|3";
        String[] values= input.split("\\|");
        System.out.println("[" + values[0] + "]");
like image 35
Hussain Akhtar Wahid 'Ghouri' Avatar answered Sep 19 '22 16:09

Hussain Akhtar Wahid 'Ghouri'