Simple String Split [duplicate] - java

This question already has answers here:
split not working correctly
(4 answers)
Closed 9 years ago.
Clearly, I am losing my mind. I have the following string:
Tuesday|6:00 pm|Sub 10:00|Name
I want to split that into 4 tokens using the "|" as a delimiter. Easy, right?
String myString = "Tuesday|6:00 pm|Sub 10:00|Name";
System.err.println(myString);
String[] tokens = myString.split("|");
for (int i=0; i<4; i++) System.err.println(tokens[i]);
And here is my output:
Tuesday|6:00 pm|Sub 10:00|Name
T
u
e
What am I missing? (this is going to be one of those 'Doh!' moments, I predict.) Thank you!

split takes a regular expression as its argument. The pipe character | is a meta-character in regular expressions which denotes OR. It needs to be escaped
String[] tokens = myString.split("\\|");
otherwise the String is split into its individual characters

Related

Splitting a string in which delimiting string ^^^^ has special meaning for regex [duplicate]

This question already has answers here:
Java split on ^ (caret?) not working, is this a special character?
(5 answers)
Closed 3 years ago.
Trying to split a string like this
12^^^^John Doe^^^^+1897987987
delimiter is the ^^^^
split method does not split and instead returns entire line and single length array
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
^ has a special meaning in regex so I believe this split params have to be passed in a different way to achieve the desired result, please advise.
You're going to need to escape those characters - ^ means "start of line" in a regex.
String[] line = str.split("\\^\\^\\^\\^");
Two \ are needed because otherwise Java tries interpreting \^ as a string escape character, like \n or \t.

Split a string into substring using '|' character [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 3 years ago.
I am trying to split a string as follows, "Client|Role". My regex is "|".
I expected two substrings as "Client" and "Role", but what I get is each and every character as a substring including the character in my regex. Why I am getting like this? Please help, Thanks in advance.
My code is as follows,
String output = "Client|Role";
String values[] = output.split("|");
You can split like this..
String b = "Client|Role";
String[] elements = b.split("\\|");

java.lang.ArrayIndexOutOfBoundsException after two splits [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 6 years ago.
I want to split a string three times.
This is the string:
21.06.2016;00:30
My function looks like this:
String[] split = dateV.split(";");
String[] date = split[0].split(".");
String[] time = split[1].split(":");
date[0] should contain "21" after all
So the first part works great.
My two strings are
split[0] = 21.06.2016
split[1] = 00:30
But when I call split[0].split("."); I get a
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
Can somebody tell me why?
String.split uses regular expressions to do the split, and a dot is a special character when regular expressions are used.
To split using a dot, you need to escape it like this
String[] date = split[0].split("\\.");

How to convert dot (.) separated string into array list? [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
In my application I need to convert dot separated string to arraylist. I have seen in many places split function of string is used to separate comma separated string to Array List.I studied in the documentation split function requires expression for splitting.In my case I provided dot and it is not converting.Am I missing something? Please help me.Below is my code I am using
Here,
groupHierarchy.levelPrefix = 4201.4202;
ArrayList<String> levelPrefixList = new ArrayList<>(
Arrays.asList(groupHierarchy.levelPrefix.split(".")));
When I log this it shows
Log.e("tracking","level prefix list is "+ levelPrefixList);
Use following:
groupHierarchy.levelPrefix.split("\\.")
public String[] split(String regex) use regex, so you have to escape "."
See Split String on . as delimiter for more details.

How to split string in java when there are multiple criteria to split [duplicate]

This question already has an answer here:
Java string.split - by multiple character delimiter [duplicate]
(1 answer)
Closed 9 years ago.
To split a paragraph into an array of individual words (say a string array), the most common answer would be something like:
String para = "ameya needs to win this cup.";
String[] abc = para.split(" ");
However if the para included ? and ,'s and ; etc, how can this be done?
For eg:
String para = "ameya,needs. to win?this cup.";
String#split(arg) takes regex as argument, and in regex if you want to match one of many characters then you can use this form (a|b|c|...|z) which means character that is eater a OR b OR c OR [...] OR z (instead of ... you actually need to put rest of alphabet letters).
But since that form is ugly you can use character class that can look like [abcd...z]. But this can also be optimized a little using range of characters [a-z].
Now lets go back to your question. If you want to match all spaces and additional characters then you can try to split on every [\\s.,;?]. Also in case you want to split on single group of that characters you can use [\\s.,;?]+. + means one or more elements that are described before +.
So maybe try this way
String[] abc = para.split("[\\s.,;?]+");
Use a regular expression
String str = "ameya,needs. to win?this cup.";
String [] arr = str.split("[\\s|,|\\.|;|\\?]");

Categories

Resources