Java split String at | [duplicate] - java

This question already has answers here:
Splitting a String with the pipe symbol as delimiter
(6 answers)
Closed 7 years ago.
How can I split my String after this character: |
If i simply write:
String[] parts = match.split("|");
the String is split after every single Character.

Please, escape the character:
String[] parts = match.split("\\|");

Use:
String[] parts = match.split("\\|");
The pipe symbol is a special character for regular expressions; you need to escape it with a backslash if you want to use the literal pipe symbol character. And because the backslash is a special character in Java strings, you need to escape that too with another backslash. Hence, the double backslash before the pipe symbol.

String.split() receives regular expression where | has special meaning. If you want to split by | you have to escape it using back slash \:
String.split("\\|")
The double back slash is needed here to escape the back slash from the point of view of java, and then escape the | from the point of view of regex.

The recommended method is to use:
String[] parts = match.split(Pattern.quote("|"));
this
public void test() {
String match = "A|B";
String[] parts = match.split(Pattern.quote("|"));
System.out.println(Arrays.toString(parts));
}
prints
[A, B]

You need to escape this character. So you can use this :-
String[] parts = match.split("\\|");
So if match is not containing | , the parts will contain only single element , ie match, else the splitted match

Related

Java String splitting difference between "|" and "\|" [duplicate]

I have a String = "Hello-new-World". And when i use the split() method with different regex values, it acts differently.
String str = "Hello-new-world"
String[] strbuf=str.split("-");
for(int i=0;i<strbuf.length;i++)
System.out.print(strbuf[i]+" ");
The output i get is :
hello
new
world
whereas if i change my string to "Hello|new|world", i get an altogether different answer. The new output becomes:
h
e
l
l
o
|
n
e
w
|
w
o
r
l
d
Can someone please explain what could be the possible reason for this.
Presumably you're splitting on "|" in the second case - and | has a special meaning within regular expressions. If you want to split on the actual pipe character, you should escape it:
String[] bits = whole.split(Pattern.quote("|"));
split method takes regular expression as input. The pipe is a special character for regular expression, so if you want to use it tou need to escape the special character. Ther are multiple solutions:
You need to escape the "pipe" character
str.split("\\|");
Or you can use the helper quote:
str.split(Regexp.quote("|"))
Or between sqares:
str.split("[|]");
Pipe is special regex symbol which means OR, if you want to split by pipe then escape it in your regex:
String[] strbuf = str.split("\\|");
OR
String[] strbuf = str.split("[|]");
str.split("|");
means something different. String#split uses regex, and | is a metacharacter, so that string means: split off of the empty string or off of the empty string. That is why your string gets split on every character.
There are a few ways of doing what you expect (use these as the string to split off of):
"\\|"
Which means to escape the metacharacter.
"[|]"
Puts the metacharacter in a character class.
"\\Q|\\E"
Puts the metacharacter in a quote
An unescaped | is parsed as a regex meaning "empty string or empty string," so use
str.split("\\|");
| having special meaning OR in regex
The pipe has different meaning in regular expression, so if you want to use it you need to escape the special character.
str.split("\\|");
It is a metacharacter. Escape it with a backslash, like this: "\\|"

Java doesn't split "|" symbol correctly [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a file with content
1|yes|
2|yes|
3|yes|
4|yes|
5|yes|
6|yes|
7|yes|
8|yes|
9|yes|
10|yes|
11|yes|
12|yes|
13|yes|
14|yes|
15|yes|
I use java's String[] tokens = split("|"); to split each line, but it returns (for example splitting "10|yes|") [1,0,|,y,e,s,|]. It seems instead of splitting by "|", it splits every character. Anyone has any idea on it? Thanks!
split accepts a regular expression. | has a specific meaning in regular expressions, it expresses an alternation. To actually split on |, you have to escape it in the regex with a backslash. Since you specify the regex using a string literal, and backslashes are special in string literals, you have to escape that with another backslash:
String[] tokens = str.split("\\|");
In the general case, if you want to use the contents of a string literally, you can use Pattern.quote to automatically escape any special characters. You don't really need it here, but it's useful for end-user-entered values:
String[] tokens = str.split(Pattern.quote(stringToSplitOnLiterally));

Is it possible to use split function with a '.' as a delimiting regular expression? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
I wanna split the content of a string variable, but I wanna use the point as a delimiting regular expression, my code doesn't work.
public class Test {
public static void main(String [] a){
String ch = "r.a.c.h.i.d";
String[] tab;
tab=ch.split(".");
System.out.println(tab.length);
for(String e : tab)System.out.println(e);
}
}
Change tab=ch.split("."); to tab=ch.split("\\.");. You need to escape the dot because otherwise it's treated as a special character in the regex passed to split.
tab = ch.split("\\.");
One slash is the escape character for the regex. But in Java you need to have a second slash because you have to escape the first slash.
Yes, it's possible. In a regular expression, . means any character.
Predefined character classes
.       Any character (may or may not match line terminators)
So you must escape it to provide the literal . meaning. Escape it with a backslash character, providing two backslashes, because Java needs the backslash character itself escaped.
Use the regular expression "\\.".
In general, to get the literal characters out of an expression that may contain special-meaning characters, you can use the Pattern.quote method.
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
split(Pattern.quote("."))

String.Split - Unexpected behaviour [duplicate]

This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Closed 8 years ago.
I am trying to split a string as follows
String string = "mike|ricki"
If I do the following string.split("|") I would expect an array of 2 elements, "mike" and "ricki". Instead I am getting the following
[, m, i, k, e, |, r, i, c, k, i]
Am i doing something fundamentally wrong here?
Yes. Pipe character | is a special character in regular expressions. You must escape it by using \. The escape string would be \|, but in Java the backslash \ is a special character for escape in literal Strings, so you have to double escape it and use \\|:
String[] names = string.split("\\|");
System.out.println(Arrays.toString(names));
If you read the String.split() Java Documentation, it says that it can receive a Regular Expression as an input.
The Pipe character | is a special character in regular expressions so if you want to use it as a literal you have to escape it like \\|
So your code have to be:
String[] splitted = string.split("\\|");
EDIT : Corrected sample code.
String.split takes a regular expression. The pipe character has a special meaning in regex so it's not matching as you were expecting.
Try String.split("\\|") instead.
The backslash tells regex to treat the pipe as a literal character.

Splitting a String in Java

I have a string as follows
String s = "3|4||5 9|4 0|0 4 8|..."
and I want to split it based on the "|" appearances. As such, the split should return
["3","4","5 9","4 0,"0 4 8",...]
But, in Java,
s.split("|") = [, 3, |, 4, ...]
In other words, it is splitting by the "" character, it seems. What is wrong?
The | character has special meaning in regular expressions, so you must escape it with a backslash. Then you must escape the backslash itself for Java. Try:
s.split("\\|")
The Javadocs for the Pattern class has lots of details about special characters in regular expressions. See the "Logical operators" section in that page for what | does.
Note that public String[] split(String regex) takes a regex.
Since | is a meta character,It works when you escape the special character.
String[] results = result.split("\\|");
or(personally recommending this)
String[] result = result.split(Pattern.quote("|"));
If you use Pattern
Now, | will be treated as normal character | and not as the regex meta char |.
Oracle explained here why \\
You can try like below
s.split("[|]")

Categories