Prevent Empty values on string Split
I would replace the separating characters with spaces. Then you can use trim() to remove any extra characters at either end before you split. I'm assuming you don't want any empty strings in the results, so I've changed the expression slightly.
ab.replaceAll("\\W+", " ").trim().split(" ")
Java String split removed empty values
split(delimiter)
by default removes trailing empty strings from result array. To turn this mechanism off we need to use overloaded version of split(delimiter, limit)
with limit
set to negative value like
String[] split = data.split("\\|", -1);
Little more details:split(regex)
internally returns result of split(regex, 0)
and in documentation of this method you can find (emphasis mine)
The
limit
parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.If the limit
n
is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.If
n
is non-positive then the pattern will be applied as many times as possible and the array can have any length.If
n
is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
Exception:
It is worth mentioning that removing trailing empty string makes sense only if such empty strings were created by the split mechanism. So for "".split(anything)
since we can't split ""
farther we will get as result [""]
array.
It happens because split didn't happen here, so ""
despite being empty and trailing represents original string, not empty string which was created by splitting process.
How to split string without getting empty values to the output array
You can use match
with regex.
str.match(/[^\/]+/g);
The regex [^\/]+
will match any character that is not forward slash.
function getValues(str) { return str.match(/[^\/]+/g);}
document.write('<pre>');document.write('<b>/value/1/</b><br />' + JSON.stringify(getValues('/value/1/'), 0, 4));document.write('<br /><br /><b>/value/1</b><br />' + JSON.stringify(getValues('/value/1'), 0, 4));document.write('<br /><br /><b>/value/</b><br />' + JSON.stringify(getValues('/value/'), 0, 4));document.write('<br /><br /><b>/value</b><br />' + JSON.stringify(getValues('/value'), 0, 4));document.write('</pre>');
How to prevent java.lang.String.split() from creating a leading empty string?
Your best bet is probably just to strip out any leading delimiter:
String input = "/Test/Stuff";
String[] test = input.replaceFirst("^/", "").split("/");
You can make it more generic by putting it in a method:
public String[] mySplit(final String input, final String delim)
{
return input.replaceFirst("^" + delim, "").split(delim);
}
String[] test = mySplit("/Test/Stuff", "/");
Java String.split(), how to prevent empty element in the new array
You could use a quantifier
String[] array = "hello.are..you".split("\\.+");
To handle a leading .
character you could do:
String[] array = ".hello.are..you".replaceAll("^\\.", "").split("\\.+");
String.split() ignoring empty values inbetween delimiters if on the final part of a string
By default split
removes trailing empty strings from result array. To turn off this mechanism use split(regex, limit)
with negative limit like
split("\\|", -1)
Little more details:split(regex)
internally returns result of split(regex, 0)
and in documentation of this method you can find (emphasis mine)
The
limit
parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.If the limit
n
is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.If
n
is non-positive then the pattern will be applied as many times as possible and the array can have any length.If
n
is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
How to split String without leaving behind empty strings?
Add the "one or more times" greediness quantifier to your character class:
String[] inputTokens = input.split("[(),\\s]+");
This will result in one leading empty String, which is unavoidable when using the split()
method and splitting away the immediate start of the String and otherwise no empty Strings.
Getting rid of null/empty string values in a C# array
Try this:
yourString.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
Related Topics
Multiple Queries Executed in Java in Single Statement
How to Return a Custom Object from a Spring Data JPA Group by Query
Find Maximum Score or the Maximum Average Score of Candidate Scores Given in Two Dim Array
How to Get the Path of Src/Test/Resources Directory in Junit
How to Autowire a Component Which Is Having Constructor With Arguments in Springboot Application
How to Save a File Downloaded With Httpclient into a Specific Folder
How to Upload a Document to Sharepoint With Java
Spring Boot @Autowired Environment Throws Nullpointerexception
Passing Data into HTML from Java Via Spring
Springboot - How to Start Embedded Container
How to Return Different Results When Calling the Same Mocked Method
Map Json String Column of a JPA Entity to Java Object Automatically
Method Does Not Override Method from Its Superclass . Android Fragment
How to Loop Through List of Webelements and Select One Webelement With a Condition
How to Sort Json Object in Java
Broadcast Receiver Is Not Working When Application Is Removed from Background