Java String Split: "Ignore" A Split Parameter [duplicate] - java

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Closed 9 years ago.
I'm trying to split a string every time there is a comma in it.
String myString = "\"Shire, Middle Earth\",Hobbits, J.R.R Tolkien";
My string, when printed out is: "Shire, Middle Earth",Hobbits, J.R.R. Tolkien
Notice that there is a space between Shire and Middle Earth
When I do the following...
String[] myString = line.split(",");
It counts the comma between the Shire and Middle Earth as a comma to split the data a (as it should). How can I get it to "ignore" that comma?

try this:
line.split(",(?!\\s)")
This is lookahead in regex. You can see this link here

Related

How do I use positive and negative lookbehinds to match only if a certain pattern doesn't match before and after it? [duplicate]

This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in quotes
(12 answers)
Closed 2 years ago.
I am trying to split a string by comma, however I don't want to split if the comma is surrounded by quotes.
Here is what I am trying to do:
String s = "today,\"is, clear\",and sunny";
String[] split = s.split("(?>!\"[0-z]*),(?![0-z]*\")");
And the contents of split would be ["today", "\"is, clear\"", "and sunny"], but I'm having trouble getting the regex to work right.
One option would be to use a negative lookahead which asserts that a split on comma only happens when an even number of double quotes follows. Appreciate that commas inside double quotes would always have an odd number of double quotes following, assuming your quotes are always balanced.
String s = "today,\"is, clear\",and sunny";
String[] parts = s.split(",(?!.*\"(?:.*\".*\")*$)");
System.out.println(Arrays.toString(parts));
This prints:
[today, "is, clear", and sunny]

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.

Why do I get blank spaces after splitting the string by dot? [duplicate]

This question already has answers here:
Java string split with "." (dot) [duplicate]
(4 answers)
Closed 8 years ago.
I am trying to split a String by . :
String sentences[] = fileContent.split(".");
fileContent is a string that contains the complete text data from a file. In the file there are 4 sentences some separated by white space gaps.
When I print sentences[n] it gives a blank . Why is it so when sentences.length prints 95.
Data structured in fileContent looks like : (no-meaning text)
My name is suhail. His name is suhail.
He was playing with suhail. He is cool and loves suhail.
You have to escape the dot character, because split() expects a regex (regular expression), not a plain string:
String sentences[] = fileContent.split("\\.");

Categories

Resources