Parsing a string works not as expected [duplicate] - java

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 5 years ago.
There is a string which I trying to parse by "|" symbol:
1-20|21-40|41-60|61-80|81-100|101-120|121-131
String[] arr = text.split("|");
for(int i = 0; i <arr.length; i++){
System.out.println( arr[i] );
}
It parses to every character, like
1
-
2
0
|
2
1
...
How to parse the source string for elements like:
1-20

| is a special character in Java's regex syntax that means a logical "or" between two matching groups. If you want to match the | literal, you need to escape it:
String[] arr = text.split("\\|");

This | is a special character in regular expression(s), you need to escape it. Like,
String[] arr = text.split("\\|");

| is a metacaracter in regex. Escape it:
String[] splitValues = text.split("\\|");

escape the pipe using "\\|"
String[] arr = text.split("\\|");

Related

Split a word by a char in Java [duplicate]

This question already has answers here:
How to split a string, but also keep the delimiters?
(24 answers)
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
Consider the following example. I would like to divide the String into two parts by the char 'T'
// input
String toDivideStr = "RaT15544";
// output
first= "RaT";
second = "15544";
I've tried this:
String[] first = toDivideStr.split("T",0);
Output:
first = "Ra"
second = "15544"
How do I achieve this?
What you need to to, is locate the last "T", then split:
StringToD.substring(StringToD.lastIndexOf("T") + 1)
You could use a positive lookahead to assert a digit and a positive lookbehind to assert RaT.
(?<=RaT)(?=\\d)
For example:
String str = "RaT15544";
for (String element : str.split("(?<=RaT)(?=\\d)"))
System.out.println(element);
Regex demo | Java demo
You can use positive look-ahead with split limit parameter for this. (?=\\d)
With only T in the split method parameter, what happens is the regex engine consumes this T. Hence the two string split that occurs doesn't have T. To avoid consuming the characters, we can use non-consumeing look-ahead.
(?=\\d) - This will match the first number that is encountered but it will not consume this number
public static void main(String[] args) {
String s = "RaT15544";
String[] ss = s.split("(?=\\d)", 2);
System.out.println(ss[0] + " " + ss[1]);
}
The below regex can be used to split the alphabets and numbers separately.
String StringToD = "RaT15544";
String[] parts = StringToD.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
System.out.println(parts[0]);
System.out.println(parts[1]);

How to Split String with delimited string "#|#" [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 5 years ago.
String line = "First string March 8, # 2017: Boris#|#Second string";
String[] list = line.split("#|#");
i was expecting list[0] = "First string March 8, # 2017: Boris" and
list[1] = "Second string"
But i am not getting the result as expected . its get split to multiple strings. whats the change i need to do in split function ?
String[] list = line.split("#\\|#");
The split() method's (first) parameter is expected to contain a regular expression. The | is a special character is Regex, so you need to escape it with \ to represent it in a regex literally.
You need to escape the pipe: #\\|#
example:
String line = "First string March 8, # 2017: Boris#|#Second string";
String[] list = line.split("#\\|#");
System.out.println(Arrays.toString(list));
The split() method doesn't expect normal strings, but regular expressions. Thus you need to escape the | char; so go for:
split("#\\|#");

Can't split text that contain "|" in String [duplicate]

This question already has answers here:
How to split a string on | (pipe) in Java [duplicate]
(3 answers)
Closed 7 years ago.
I'm wondering why I cannot split a text that contain the | as a separator in String. The splitting works fine when I use commas or the like..
Here is an SSCE
package tests;
public class Tests {
public static void main(String[] args) {
String text1="one|two|three|four|five";
String text2="one,two,three,four,five";
String [] splittedText1 = text1.split("|");
String [] splittedText2 = text2.split(",");
for(String elem : splittedText1) System.out.println("text1="+elem);
for(String elem : splittedText2) System.out.println("text2="+elem);
}
}
Any ideas why it doesn't work with "|" ??
Since split(String regex) takes a regex and | is a meta character, you need to escape it.
String[] splittedText1 = splittedText1.split("\\|");
Or you can simply use Pattern class
A compiled representation of a regular expression.
String[] splittedText1 = splittedText1.split(Pattern.quote("|"));
Because the split pattern is actually a regex. You need to escape |, since it has a special meaning in the context of a regular expression (it marks an alternative):
String [] splittedText1 = text1.split("\\|");

Different behaviour when split a string using | and / [duplicate]

This question already has answers here:
Java Strange split behavior with | character
(2 answers)
Closed 7 years ago.
When i split the String as below,
String s1 = "id::34|desc::test";
String s2 = "id::34/desc::test";
String [] s1a = s1.split("|");
String [] s2a = s2.split("/");
Why the s1a array contains elements for each character of s1 where s2a only has two elements which are id::34 and desc::test.
I am expecting s1a also have two elements.
String.split() takes a regex. | is a special character in regex engine, you need to escape it using \\| or use Pattern.quote().

Java String.split mehod don't work well [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Split string with | separator in java
I'm little confused as when i do the following:
String example1 = "Hello|World";
String[] splitRes;
splitRes = example1.split("|");
I don't get split string
Hello index 0
World index 1
But if I'll do
String example1 = "Hello:World";
String[] splitRes;
splitRes = example1.split(":");
then it works..
Why is it happening?
split uses a regex, you must escape the pipe because it is a "or" operator in regex:
example1.split("\\|");
String.split() expects regular expression as argument, | is a meta character "OR" in regular expression. You have to escape with \ (so it becomes \|). Note that in Java string, you have to write it as \\ since \ is also an escape character in Java string.
| is used in regular expression, .split also use regular expression so you need to escape it.
String str = ""Hello:World"; ";
String[] temp;
String delimiter = "\\|";
SepString= str.split(delimiter);
/* print test */
for(int i =0; i < SepString.length ; i++)
System.out.println(SepString[i]);
Split takes a regexp as an argument, | is a a regexp symbol.
You would have to escape it using \ which in a java string is two of them: \\
.split("\\|");

Categories