Java String.split() drops trailing empty strings (use limit -1)
Codemia Knowledge Hub ·
String.split() does not remove all empty values. The one-argument form removes only trailing empty strings from the result; empty strings in the middle are kept. To keep trailing empty values too, pass -1 as the second argument: split(delimiter, -1).
This catches many developers off guard when parsing delimited data where empty fields carry meaning.
How the limit parameter works
split has two overloads:
The one-argument form behaves as if you passed limit = 0. The limit controls how many times the pattern is applied and whether trailing empty strings are kept.
| Limit Value | Pattern Applications | Trailing Empty Strings |
0 (default) | Unlimited | Removed |
-1 | Unlimited | Preserved |
n > 0 | At most n - 1 times | Preserved |
Default behavior (limit = 0)
Negative limit (limit = -1)
Positive limit (limit = 3)
Middle empty values are always kept
Only trailing empty strings are affected by the default limit. Empty strings between non-empty values survive either way:
This asymmetry is the root of most confusion: people expect every empty field to be treated the same, but only trailing ones are dropped.
The delimiter is a regular expression
split() treats its first argument as a regex, not a plain string, so characters with special meaning must be escaped. Splitting on an unescaped . matches every character and produces an empty array, because every element is a trailing empty string:
Characters that need escaping in regex: ., |, *, +, ?, (, ), [, ], {, }, ^, $, \.
Pattern.quote for dynamic delimiters
When the delimiter comes from user input or configuration, hand-escaping is error-prone. Pattern.quote() treats any string as a literal:
Where trailing empties matter
Parsing delimited rows
In pipe- or tab-delimited exports, a trailing empty field is real data, such as a nullable column with no value. The default split drops it, so code that reads fields by position gets fewer elements than expected and can throw ArrayIndexOutOfBoundsException.
Splitting configuration values
A positive limit keeps everything after the first = together, so an = inside the value does not cause another split:
Counting segments
split vs. StringTokenizer vs. Guava Splitter
| Feature | String.split() | StringTokenizer | Guava Splitter |
| Trailing empties | Dropped by default | Always dropped | Configurable |
| Regex support | Yes | No | Optional |
| Empty token handling | Middle preserved | Always dropped | Configurable |
| Return type | String[] | Enumeration | Iterable<String> |
| Thread-safe | Yes (immutable) | No | Yes (immutable) |
Guava's Splitter gives the most explicit control over empty values:
When to use a real parser instead
split() works for simple delimited data where fields never contain the delimiter. It breaks when the format supports quoting or escaping:
For CSV, TSV with quoting, multiline fields or any structured text format, use a dedicated parser such as OpenCSV, Apache Commons CSV or Jackson CSV. split() is not a format parser.
Also validate the number of fields when you split user input with split(delimiter, -1). Code that assumes a fixed field count can introduce injection risks.
Related reading
- Java string split with . (dot)
- Java string to date conversion
- Java String to SHA1
- Java sun.security.provider.certpath.SunCertPathBuilderException unable to find valid certification path to requested target
- Java Swing revalidate vs repaint
- Java switch statement Constant expression required, but it IS constant
- Java Synchronized Block for .class
- Java synchronized method
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.