Homeβ€Ί Javaβ€Ί Java Split String: By Delimiter, Regex and Limit

Java Split String: By Delimiter, Regex and Limit

Where developers are forged. Β· Structured learning Β· Free forever.
πŸ“ Part of: Strings β†’ Topic 14 of 15
Master Java String.
πŸ§‘β€πŸ’» Beginner-friendly β€” no prior Java experience needed
In this tutorial, you'll learn:
  • 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.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
⚑ Quick Answer
String.split() cuts a string into pieces wherever it finds your delimiter. The subtlety is that the delimiter is always treated as a regular expression, not a literal string β€” which means characters like '.', '|', and '+' need escaping. And the default behaviour silently discards trailing empty strings, which trips up CSV parsing.

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

StringSplitExample.java Β· JAVA
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
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]
    }
}
β–Ά Output
[PaymentService, OrderService, AuditService]
[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 CharacterMust Escape?Use
.Yes β€” \\.Split by literal dot
|Yes β€” \\|Split by literal pipe
+Yes β€” \\+Split by literal plus
,NoSplit by comma
NoSplit by single space
\\s+N/ASplit 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.

πŸ”₯
Naren Founder & Author

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.

← PreviousJava String contains(): Check for SubstringsNext β†’Java String replace(), replaceAll() and replaceFirst()
Forged with πŸ”₯ at TheCodeForge.io β€” Where Developers Are Forged