Nick ODell
Nick ODell

Reputation: 25210

Java's String.split() drops trailing empty entries

I'm trying to parse some pipe delimited records. I've got this code:

public class Test {
    public static void main(String[] args) throws Exception {
        String[] components = "EN|1056209|||KA3NDL|L|||||||||||||||||||||".split("\\|");
        System.out.println(java.util.Arrays.toString(components));
    }
}

You'd expect it to print:

[EN, 1056209, , , KA3NDL, L, , , , , , , , , , , , , , , , , , , , , ]

But it doesn't. It prints:

[EN, 1056209, , , KA3NDL, L]

Accessing the length attribute of the components array shows it's being printed correctly.

Anybody know why this is/a workaround?

EDIT:

This works:

public class Test {
    public static void main(String[] args) throws Exception {
        String[] components = "EN|1056209|||KA3NDL|L|||||||||||||||||||||".split("\\|", -1);
        System.out.println(java.util.Arrays.toString(components));
    }
}

Upvotes: 3

Views: 1161

Answers (4)

pb2q
pb2q

Reputation: 59607

String.split without the limit argument drops all trailing empty Strings from the result array. You'll need to include a limit:

String str = "EN|1056209|||KA3NDL|L|||||||||||||||||||||";
String[] components = str.split("\\|", -1);

From String.split(String, int):

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.

Calling String.split(String) is equivalent to calling the 2-argument split with a limit of 0.

Upvotes: 15

Reimeus
Reimeus

Reputation: 159754

Have at look at Splitter in the excellent Guava Libraries:

String input = "EN|1056209|||KA3NDL|L|||||||||||||||||||||";
Iterable<String> splitIter = Splitter.on("|").split(input);
System.out.println(splitIter);

output:

[EN, 1056209, , , KA3NDL, L, , , , , , , , , , , , , , , , , , , , , ]

Upvotes: 1

Marimuthu Madasamy
Marimuthu Madasamy

Reputation: 13531

You can pass a limit to the split method so that the trailing empty strings are retained:

"EN|1056209|||KA3NDL|L|||||||||||||||||||||".split("\\|", -1)

Upvotes: 4

Jochen
Jochen

Reputation: 2295

Use the two-argument version of split() instead. It allows you to configure whether or not empty string should be returned as well.

Upvotes: 2

Related Questions