Java Split String Consecutive Delimiters

String.split leaves an empty string ("") where it encounters consecutive delimiters, as long as you use the right regex. If you want to replace it with "empty", you'd have to do so yourself:

String[] split = barcodeFields.split("\\^");
for (int i = 0; i < split.length; ++i) {
    if (split[i].length() == 0) {
        split[i] = "empty";
    }
}

The answer by mureinik is quite close, but wrong in an important edge case: when the trailing delimiters are in the end. To account for that you have to use:

contents.split("\\^", -1)

E.g. look at the following code:

final String line = "alpha ^beta ^^^";
List<String> fieldsA = Arrays.asList(line.split("\\^"));
List<String> fieldsB = Arrays.asList(line.split("\\^", -1));
System.out.printf("# of fieldsA is: %d\n", fieldsA.size());
System.out.printf("# of fieldsB is: %d\n", fieldsB.size());

The above prints:

# of fieldsA is: 2
# of fieldsB is: 5

Using ^+ means one (or more consecutive) carat characters. Remove the plus

String[] barcodeFields = contents.split("\\^");

and it won't eat empty fields. You'll get (your requested) "" for empty fields.

Tags:

Java

Regex