18

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?

1
  • If you were using Guava, Splitter.on(",").split(",,,") produces the expected result. See e.g. this answer for more. Commented Sep 2, 2015 at 21:10

1 Answer 1

37

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

Sign up to request clarification or add additional context in comments.

3 Comments

Care to explain this for the above example? If I'm not asking for too much.
What does it mean by "applied as many times as possible"? How does that give us an array of empty strings?
AnjanBaradwaj, @GIJoe - I've updated the answer with a little more explanation and the source code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.