Explain Codes LogoExplain Codes Logo

Java String split removed empty values

java
regex
string-manipulation
best-practices
Alex KataevbyAlex Kataev·Sep 2, 2024
TLDR

To include empty strings in your array when splitting a string with consecutive delimiters, pass a negative limit to String.split():

// "Time to split, says the comma" String[] parts = "a,little,bit,,".split(",", -1);

This approach excludes automatic removal of trailing empty values that ordinarily occur with split().

Dealing with special characters in split delimiter

Special characters in a string are given specific functions in regular expressions (regex). To split a string at a |, it must be escaped using a backslash (\). Since backslashes are also required to be escaped in Java strings, the result is:

// "Pipe down, you're not special!" - Backslash String[] parts = "a|b|c".split("\\|", -1);

Forgetting to escape special characters such as | can lead to unexpected results or errors.

Trimming unwanted leading or trailing delimiters

To avoid counting leading or trailing delimiters as empty values in the array, trim() before splitting:

// "We're not letting any delimiters slip through the cracks" String[] parts = ",a,peaceful,world,".trim().split(",", -1);

But beware, trim() only removes leading and trailing whitespace, not other types of delimiters.

Working with whitespaces as delimiters

While splitting on characters such as spaces, tabs, and line breaks, the regex \s+ matches one or more whitespace characters:

// "Guess you could say these strings just need a little space" String[] parts = "a b\tc\nd".split("\\s+");

Note that without a '+', \s would split at each individual whitespace character, leading to empty strings if consecutive whitespace characters are present.

Recognizing the significance of negative limit in split

The split() method's second parameter controls how the method handles empty strings between delimiters. A negative limit means "don't remove any empty strings":

// "No strings left behind!" - Split() String[] parts = "a::b::c::".split("::", -1); // retains trailing empty strings

If the limit is 0 or not specified, split() removes trailing empty strings but keeps others:

// "If I can't see it, it doesn't exist!" - Split() probably String[] parts = "a::b::c::".split("::"); // removes trailing empty strings

A positive limit, however, restricts the maximum items in the array:

// "One and one is too darn high!" - Split() String[] parts = "a::b::c".split("::", 2); // only splits the first delimiter, leaving b::c intact

When to use split() with a limit

Always specify a limit when the last elements might be empty but they are important for your data:

// "Don't you dare leave key3 alone!" - Key-value police String input = "key1=value1;key2=value2;key3="; String[] keyValuePairs = input.split(";", -1); // keys with empty values are important too!

Case of an empty original string

When the original string is empty or consists only of the delimitters, split() without a negative limit returns an empty array:

// "Gosh! There's nothing here." - Sherlock Holmes, when looking at an empty string String[] parts = ",".split(",");

Using a negative limit in this case results in an array of two empty strings:

// "Aha! I found.... nothing?" - Sherlock Holmes, outsmarted by a negative limit String[] parts = ",".split(",", -1);