2

Java Experts ,

Please look into the below split command code and let me know why last two nulls are not captured.

String test = "1,O1,,,,0.0000,0.0000,,";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
    System.out.println("value"+string);
}

The result iam getting

length -7
value1
valueO1
value
value
value
value0.0000
value0.0000

surprisingly the length is 7 where as it should be 9 and also as you can see values after 0.0000 ie two last nulls are not coming . Lets say now if i change the string test "1,O1,,,,0.0000,0.0000,0,0"

String test = "1,O1,,,,0.0000,0.0000,0,0";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
    System.out.println("value"+string);
}

I am getting correctly

length -9
value1
valueO1
value
value
value
value0.0000
value0.0000
value0
value0

I don't think iam doing wrong . Is it a bug ? JAVA Version - jdk1.6.0_31

2
  • Your use of split seems fine...I don't believe that this is a bug at all. Commented Jul 10, 2012 at 16:49
  • Don't get me wrong, there's no problem with your asking for clarification on why your program is behaving the way it's behaving, but there is a little bit wrong with your assuming that it's not a problem with your code. Realistically, the chances of you being right and the Java coding engineers being wrong are just so astronomically small that this shouldn't even enter into the conversation until much later. Commented Jul 10, 2012 at 16:59

3 Answers 3

17

It behaves as specified in the javadoc:

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.

If you want to keep the trailing blank strings, you can use the 2 argument split method with a negative limit:

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

If the limit is non-positive then the pattern will be applied as many times as possible and the array can have any length.

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

Comments

7

split silently discards trailing separators, as specified in the Javadoc.

In general, the behavior of split is kind of weird. Consider using Guava's Splitter instead, which has somewhat more predictable and customizable behavior. (Disclosure: I contribute to Guava.)

Splitter.on(',').split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, , , , 0.0000, 0.0000, , ]
Splitter.on(',').omitEmptyStrings()
  .split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, 0.0000, 0.0000]

Comments

1

As mentioned above, test.split(","); will ignore trailing blank strings. You could use the two parameter method with a large second argument. However, the API also states

If n is non-positive then the pattern will be applied as many times
as possible and the array can have any length.

where n is the second argument. So if you want all the trailing strings, I would recommend

test.split(",", -1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.