0

I am using jdk 1.6, I am trying to run below code but it is not working as expected:

String[] arr = ",,,,,".split(",");

But length of arr is shown 0. I am expecting 6 elements in arr with all elements ""(blank) value.

Can anyone explain me reason behind this? Is there any Regular expression available to fulfil my requirement?

0

5 Answers 5

5

If you run

String[] arr = ",,,,,".split(",", -1);

the length of arr will be 6, because it also includes the empty String at the beginning.

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

1 Comment

+1 Sorry for the earlier misleading comment. Length will be 6 only. :)
4

It is working as specified in String#split() documentation. It clearly says:

Trailing empty strings are therefore not included in the resulting array.

If you want to store empty strings, use overloaded version - String.split(regex, limit):

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.

So, you need to pass a negative limit here:

String[] arr = ",,,,,".split(",", -1);

Comments

3

You can use String#split(String regex,int limit) method.

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.

String[] arr = ",,,,,".split(",",-1);

As per the documentation of String#split()

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

2 Comments

please justify a downvote
@StefanBeike May be because there was a typo in my first post , used 1 instead of -1 :P
1

Run below code:

    string str = "A,B,C,D,E,F,G";
    string[] arr = str.split(',');

you can get result as :

 string[0] = "A"

 string[1] = "B"

 string[2] = "C"

 string[3] = "D"

 string[4] = "E"

 string[5] = "F"

 string[6] = "G"

Comments

0

I am excepted ",,,,," there is no space between two "," so when you trying to split the string , there is no string rather that ',', and split is not count the argument as a string. What you pass in argument place that String is not calculated. so it's showing length 0.

For more information please have look oracle document .

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.