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

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().

Related

In java, can i use string split to split by character? [duplicate]

This question already has answers here:
Split string into array of character strings
(12 answers)
Closed 3 years ago.
For instance, I have the String variable abbaabbbba. I would like to use String split so each character would be separated, so it would be {a,b,b,a,a,b,b,b,b,a}. All the instructions I can find for string split say i need to have something, like a a space:
String mySplit = str.split("/");
Is there anyway to do this by character?
You can use the toCharArray method like this:
String myString = "abbaabbbba";
char[] myCharacters = myString.toCharArray();

How to split a String into 3 STRINGS [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
Can someone please tell me how to split this string?
Sample Input
String in = "Erin M Haggens";
Desired Output
String str1 = "Erin";
String str2 = "M";
String str3 = "Haggens";
Use the String.split() method.
The code myStr.split("\\s+") should work for you. Then you can use each element of the returned array as one of your outputted strings.

Parsing a string works not as expected [duplicate]

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("\\|");

How to split a string in java by using following String? [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
How to split the following string How to split following string "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG" with ',' and '#'
You can use String.split(regexp) function :
String[] array = "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG".split(",|#");
You should prefer the String.split method as mentioned by VLef,
but for the sake of completeness:
String s = "LLSlotBook17-07-2015#Friday#1#10.00AM-12.00PM#10#LMV,mCWG";
int x = s.indexOf("#");
String sub = s.substring(0, x);
will give you the first substring "LLSlotBook17-07-2015".
See: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

How to split a string on | (pipe) in Java [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 string like |serialNo|checkDelta?|checkFuture?|checkThis?|.
Now I am using the following code to split the string.
String[] splitString = str.split("|");
but when I use this I get array of string that contains each and every character, whereas I need string which contains letter like serialNo, checkDelta?, checkFuture?, checkthis?.
How to get these? Am I missing something?
You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):
str.split("\\|");
Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.
You are using a special character and will have to escape it: str.split("\\|");
Use StringTokenizer..
String str="|serialNo|checkDelta?|checkFuture?|checkThis?|"
StringTokenizer st=new StringTokenizer(str,"|",false);
String s1 = st.nextToken();
String s2 = st.nextToken();
String s3 = st.nextToken();
String s4 = st.nextToken();
s1=serialNo
s2=checkDelta?
s3=checkFuture?
s4=checkThis?
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

Categories