Java Split String: By Delimiter, Regex and Limit
- split() treats the delimiter as a regular expression. Escape special regex characters: use '\\.' for dot, '\\|' for pipe, '\\+' for plus.
- split(",") with no limit discards trailing empty strings. Use split(",", -1) to keep them β critical for structured data parsing.
- split(" ", N) limits the result to N parts, with the last part containing the remainder unsplit β useful for parsing log lines or structured text.
split() is the source of two recurring Java bugs in every codebase I've worked in: forgetting to escape the pipe character in split('|') and losing trailing empty values when parsing structured data. Both are fixable once you know they exist.
split() Examples, Regex Escaping, and the Limit Parameter
package io.thecodeforge.strings; import java.util.Arrays; public class StringSplitExample { public static void main(String[] args) { // Basic split by comma String csv = "PaymentService,OrderService,AuditService"; String[] services = csv.split(","); System.out.println(Arrays.toString(services)); // [PaymentService, OrderService, AuditService] // Split by pipe β MUST escape with \\| String piped = "101|payment|GBP|100.00"; String[] fields = piped.split("\\|"); // Not split("|") β pipe is regex OR System.out.println(Arrays.toString(fields)); // [101, payment, GBP, 100.00] // Split by dot β MUST escape with \\. String fqn = "io.thecodeforge.payment.PaymentService"; String[] parts = fqn.split("\\."); System.out.println(Arrays.toString(parts)); // [io, thecodeforge, payment, PaymentService] // Trailing empty strings β default behaviour discards them String withTrailing = "a,b,c,,,"; System.out.println(Arrays.toString(withTrailing.split(","))); // [a, b, c] β trailing empties discarded! // limit = -1 to keep trailing empty strings System.out.println(Arrays.toString(withTrailing.split(",", -1))); // [a, b, c, , , ] β trailing empties kept // limit = N to limit number of parts String logLine = "2026-03-30 ERROR PaymentService: Connection refused"; String[] parts3 = logLine.split(" ", 3); System.out.println(Arrays.toString(parts3)); // [2026-03-30, ERROR, PaymentService: Connection refused] // Third element contains the rest β no further splitting // Split by whitespace (any amount) String padded = " PaymentService OrderService "; String[] trimmed = padded.trim().split("\\s+"); System.out.println(Arrays.toString(trimmed)); // [PaymentService, OrderService] } }
[101, payment, GBP, 100.00]
[io, thecodeforge, payment, PaymentService]
[a, b, c]
[a, b, c, , , ]
[2026-03-30, ERROR, PaymentService: Connection refused]
[PaymentService, OrderService]
| Regex Character | Must Escape? | Use |
|---|---|---|
| . | Yes β \\. | Split by literal dot |
| | | Yes β \\| | Split by literal pipe |
| + | Yes β \\+ | Split by literal plus |
| , | No | Split by comma |
| No | Split by single space | |
| \\s+ | N/A | Split by any whitespace (1 or more) |
π― Key Takeaways
- split() treats the delimiter as a regular expression. Escape special regex characters: use '\\.' for dot, '\\|' for pipe, '\\+' for plus.
- split(",") with no limit discards trailing empty strings. Use split(",", -1) to keep them β critical for structured data parsing.
- split(" ", N) limits the result to N parts, with the last part containing the remainder unsplit β useful for parsing log lines or structured text.
- For CSV files with quotes, escape characters, or multi-char delimiters, use Apache Commons CSV or OpenCSV instead of String.split().
β Common Mistakes to Avoid
- βsplit('.') β dot in regex means 'any character'. This splits on every single character and returns an empty array. Use split('\\.') to split on a literal dot.
- βsplit('|') β pipe in regex means OR. Use split('\\|') for literal pipe.
- βNot using limit=-1 when parsing structured data with optional trailing fields β default split silently drops trailing empty strings, which corrupts CSV parsing where empty columns at the end matter.
- βSplitting on a string that contains the delimiter inside quoted fields β String.split() has no quote awareness. Use a proper CSV library (Apache Commons CSV, OpenCSV) for real CSV parsing.
Interview Questions on This Topic
- QWhat is the result of "a.b.c".split(".") in Java?
- QHow do you keep trailing empty strings when using String.split()?
Frequently Asked Questions
How do I split a Java String by a dot?
Use split("\\.") β note the four characters. A single dot in regex means 'any character'. The double backslash is Java string escaping for a single backslash, and the dot after it makes it a literal dot in the regex.
Why does Java split() remove trailing empty strings?
By default, split() with no limit parameter or a positive limit discards trailing empty strings produced by the split. Pass -1 as the limit to keep them: split(",", -1). This matters when parsing structured data where trailing empty fields represent missing values.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.