Java String split removed empty values
To include empty strings in your array when splitting a string with consecutive delimiters, pass a negative limit to String.split()
:
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:
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:
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:
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":
If the limit is 0
or not specified, split()
removes trailing empty strings but keeps others:
A positive limit, however, restricts the maximum items in the array:
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:
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:
Using a negative limit in this case results in an array of two empty strings:
Was this article helpful?