JAY zhou
JAY zhou

Reputation: 1

Why the method String.split() split the string that have the "|" in unexpected way

When I want to split the String "aaaa|bbbbb|123456" with the method String.split("|"), the result is unexpected .

My code:

String s = "aaaa|bbbbb|123456";
String[] temp = s.split("|");
for (String str:temp) {
    System.out.println(str);
}

But the result is:

a
a
a
a
|
b
b
b
b
b
|
1
2
3
4
5
6

are there anything special with char "|" ?

Upvotes: 0

Views: 97

Answers (4)

Priyantha
Priyantha

Reputation: 5083

The Sting split(String regex) method wants a regular expression as its parameter.It splits this string around matches of the given regular expression.

A preceding backslash ("\") turns a metachacter into a literal character. Because this is also the Java escape character in strings, you need to use "\" to present the backslash character. To split a string with a literal '|' character in Java,

you must use split("\\|")

. For example,

String s="aaaa|bbbbb|123456";
String [] temp=s.split("\\|");
for(String str:temp) {
    System.out.println(str);
}

For more detals

Summary of regular-expression constructs

Upvotes: 0

Tom Taylor
Tom Taylor

Reputation: 3540

In Java the following characters has to be escaped in regular expressions

.[]{}()*+-?^$|

You could either escape the | character during split like

string.split("\\|");

You could also achieve the same using StringTokenizer as follows

String test = "abc.def.123";

StringTokenizer token = new StringTokenizer(test, "|");

while (token.hasMoreTokens()) {
    System.out.println(token.nextToken());
}

To know more about the list of characters that has to be escaped with regex pattern look at this post.

Upvotes: 1

Pons
Pons

Reputation: 1111

Use escape sequences,

String s="aaaa|bbbbb|123456";
String [] temp=s.split("\\|");
for(String str:temp) {
    System.out.println(str);
}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

You need to escape the pipe:

String[] temp = s.split("\\|");

The pipe symbol | has a special meaning in regex, used to indicate an alternation. Your original code resulted in splits happening between every character, which isn't what you want.

Upvotes: 0

Related Questions