Java Error in String Split - java

I want my program to accept search strings, for example:
blue & berry (to find both of the words)
bed | sleep | pillow(to find first one or the second one etc)
When i recieve these string into my program, i use
String.split() With "&" or "|" as separator.
String[] splited = input.split("|");
It works fine in the first case, but in the second case in separates each letter in the words, for example: b e d. Can i do something for it to be separated by words with this symbol, not just splited letter by letter?

split() is taking a regexp as argument. | means or, so you are splitting on "empty string or empty string", so it's splitting after every letter. If you want to split on "|" symbol, you have to escape it:
String[] splited = input.split("\\|");

String.split(String) uses regular expressions and | is a special character in regex. Use \| to refer to a literal |, and \\ to escape the \ for Java. Resulting in \\|.

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 split String at | [duplicate]

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

Regular expression for splitting a String while preserving whitespace

I am doing an Android project which needs to split a String into tokens while preserving whitespaces and also not to split at non-word characters like #, & etc ...
Using \b splits at any non-word character .So i need a way to split the string in the following way.
Input: (. indicates whitespace)
A.A#..A##
Desired output:
A
.
A#
..
A##
So these 5 lines are the 5 values I would like in an array or similar. That means the 4th element of the result-array contains 2 spaces.
I think this is what you want:
(?<=\S)(?=\s)|(?<=\s)(?=\S)
Debuggex Demo
Basically I'm saying "if the previous character is a non-space and the next is a space or if the previous is a space and the next is a non-space, then split".
Use StringTokenizer:
StringTokenizer st = new StringTokenizer("A.A#..A##", ".");//first argument is string you want to split, another is whitespace
while(st.hasMoreTokens())
System.out.println(st.nextToken());
output will be:
A
A#
A##
Try:
String s = "A.A#..A##";
if(s.contains("..")) | s.contains("...")) {
s.replace("..", ".");
s.replace("...", ".");
String out[] = s.split(".");
It should give you an array with Strings the way you want :)
Don't forget to replace the "." with actual spaces :)

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("[|]")

Split function not working properly

I am trying to split the string using Split function in java
String empName="employee name | employee Email";
String[] empDetails=empName.split("|");
it gives me result as
empDetails[0]="e";
empDetails[1]="m";
empDetails[2]="p";
empDetails[3]="l";
empDetails[4]="o";
empDetails[5]="y";
empDetails[6]="e";
empDetails[7]="e";
.
.
.
but when i try following code
String empName="employee name - employee Email";
String[] empDetails=empName.split("-");
it gives me
empDetails[0]="employee name ";
empDetails[1]=" employee Email";
why java split function can not split the string seperated by "|"
String#split() method accepts a regex and not a String.
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String[] empDetails=empName.split("\\|");
Update:
Handling special characters in java:OFFICIAL DOCS.
As a side note:
In java method names starts with small letters.it should be split() not Split() ..not the capital and small s
but my question is why we have to use escape in case of "|" and not for "-"
Because "|" is a regex meta-character. It means "alternation"; e.g. "A|B" means match "A" or "B". If you have problems understanding Java regexes, the javadocs for Pattern describe the complete Java regex syntax.
So when you split on "|" (without the escaping!), you are specifying that the separator is "nothing or nothing", and that matches between each character of the target string.
(For the record, "-" is also a meta-character, but only in a "[..]" character group. In other contexts it doesn't require escaping.)
You should use .split("\\|"); instead of .split("|");
Try
String[] empDetails=empName.split("\\|");

Categories