Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a comma-delimited string into an array of empty strings [duplicate]

Tags:

java

string

split

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?

like image 205
Changwang Zhang Avatar asked Mar 12 '14 11:03

Changwang Zhang


People also ask

How do you convert a comma separated string to an array?

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.

How do you split a comma separated string?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);

Which method can you use character other than comma to separate values from array?

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.


1 Answers

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

like image 145
Rahul Avatar answered Oct 07 '22 13:10

Rahul