I want to split ",,,"
to a array of 4 ""
using the String.split()
Here is my code:
String str = ",,,";
String[] tokens = str.split(",");
However, the result tokens were an an empty array: [], rather than an array of 4 ""
(["","","",""])
as I wanted.
I have tested to change the str
a little bit:
String str = ",,,1";
String[] tokens = str.split(",");
This time the result tokens were ["","","","1"]
. This is close to what I want, but I really do not want to add this "1" before doing the split.
The problem is basically, the String.split()
will return an empty array if it contains only empty elements ""
.
Can you help solve the problem?
Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.
To split a string with comma, use the split() method in Java. str. split("[,]", 0);
In that case, the split() method returns an array with the entire string as an element. In the example below, the message string doesn't have a comma (,) character.
You need to use the overloaded String#split(regex, limit)
method which takes in the limit parameter.
String[] tokens = str.split(",", -1);
From the docs(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.
Explanation: When you do not provide the limit argument or provide "zero" as the limit, the split()
discards trailing empty fields. When you provide a positive limit argument, it limits the number of fields to that particular limit. But when you provide a negative limit, the split()
method allows any number of fields and also not discarding the trailing empty fields. To be more clear, have a look at the source code of the Pattern#split(regex, limit)
which has this snippet at the end(comments have been added by me and were not present in the actual source code).
if (limit == 0) // When zero or no arg is given
while (resultSize > 0 && matchList.get(resultSize-1).equals("")) // if trailing entries are blank
resultSize--; // remove them out
Note: If you do not provide any limit argument, the split()
method without limit argument calls the overloaded split()
method like this.
public String[] split(String regex) {
return split(regex, 0);
}
And also note that, String#split(regex, limit)
internally calls the Pattern#split(regex, limit)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With