9

I am parsing a CSV file, however there is one row were the last 9 columns are empty and the string split by comma ignores the remains empty columns.

Here is code to demonstrate this:

String s="L2,,,,,,,,,,,,,,,,,,108.50,-188.04,,,,,,,,,";
String[] columns = s.split(",");
System.out.println(columns.length);

The size of columns is 20, when it should be 29. Any ideas??

3 Answers 3

23

Look at the documentation for 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.

So you need to look at the options for the other split 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.

Emphasis mine.

You need:

String[] columns = s.split(",", -1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your quick response and answer. That did the trick ;)
1

The docs of split says:

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.

split(String regex) calls the overloaded split method with a limit of 0.That's why you have an array length of 20, all the empty strings after -188.04 are discarded in the resulting array.

If you want to get all the empty trailing strings, you can give a negative limit to say that you want to apply the pattern as many times as possible.

String[] columns = s.split(",", -1); //length is 29 there

Although it's not hard to parse CSV, you may also want to look about using a CSV parser.

Comments

0

Why are you doing manually job of actual CSV parsing library? There is no benefit to trying to skimp on proper job as there are a few good options out there.

These would also handle aspects of CSV like doubled double-quotes, handling of extra white space and so forth.

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.