Java String splitWithDelimiters() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Java 21+

Definition

In Java, splitWithDelimiters(regex, limit) works like split, but the matched delimiters are kept: the result alternates between the text pieces and the delimiters that separated them.

That makes it easy to break text into tokens without losing the operators or punctuation, for example turning "12+3*45" into numbers and operators, and then rebuild the original by joining everything back together.

splitWithDelimiters() is an instance method of java.lang.String, added in Java 21. Call it on a string, as in text.splitWithDelimiters(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed. Requires Java 21 or newer.

Syntax

Pass a regex and a limit; there is no single-argument overload.

java
String[] tokens = expr.splitWithDelimiters("[+*/-]", 0);
// "1+2" gives [1, +, 2]
CallWhat it does
"a,b".splitWithDelimiters(",", 0)[a, ,, b].
"a,b,c".splitWithDelimiters(",", 2)[a, ,, b,c]: one split only.
"x;".splitWithDelimiters(";", -1)[x, ;, ].

The limit counts splits, not array elements: a limit of n > 0 applies the pattern at most n - 1 times, giving up to 2n - 1 elements. With 0, trailing empty strings are removed; with a negative value, everything is kept.

Library declarations
java
public String[] splitWithDelimiters(String regex, int limit);

Parameters

ParameterDescription
regexThe delimiting regular expression.
limitPositive caps the number of splits, 0 drops trailing empty strings, negative keeps everything.

Return Value

Return ValueDescription
String[]Text pieces and delimiters, alternating, starting with a text piece.

Java Examples

Example 1 — Split and Keep the Delimiters

Compare the result with plain split.

java
import java.util.Arrays;

public class SplitWithDelimitersBasic {
    public static void main(String[] args) {
        String text = "a,b,c";

        String[] kept = text.splitWithDelimiters(",", 0);
        System.out.println(Arrays.toString(kept));
        System.out.println(kept.length + " vs " + text.split(",").length);
        System.out.println(String.join("", kept));
    }
}

How It Works

  1. Delimiters become elements. Each comma appears in the array between the letters, which Arrays.toString prints as ,, with its own separator.
  2. More elements than split. Three pieces plus two delimiters give 5 elements, versus 3 from split.
  3. Lossless. Joining every element with an empty separator rebuilds exactly "a,b,c".

Example 2 — Tokenize an Arithmetic Expression

A character class of operators separates the numbers and keeps the operators.

java
import java.util.Arrays;

public class SplitWithDelimitersTokens {
    public static void main(String[] args) {
        String expr = "12+3*45-6";
        String[] tokens = expr.splitWithDelimiters("[+*/-]", 0);

        System.out.println(Arrays.toString(tokens));
        for (int i = 1; i < tokens.length; i += 2) {
            System.out.println("operator " + tokens[i]);
        }
    }
}

How It Works

  1. One char class for all operators. [+*/-] matches any of the four symbols; the dash is last so it is literal.
  2. Alternating tokens. Numbers land at even indexes and operators at odd indexes.
  3. Walk the operators. Starting at 1 and stepping by 2 visits +, *, and -.

Example 3 — How the Limit Changes the Result

A small limit stops early; 0 and -1 differ only in trailing empty strings.

java
import java.util.Arrays;

public class SplitWithDelimitersLimit {
    public static void main(String[] args) {
        String text = "boo:::and::foo";
        System.out.println(Arrays.toString(text.splitWithDelimiters(":+", 2)));
        System.out.println(Arrays.toString(text.splitWithDelimiters(":+", 0)));

        String list = "x;y;";
        System.out.println(list.splitWithDelimiters(";", 0).length);
        System.out.println(list.splitWithDelimiters(";", -1).length);
    }
}

How It Works

  1. Limit 2 means one split. Only the first run of colons is used, so "and::foo" stays together.
  2. Limit 0 splits everywhere. Both colon runs become elements, giving five entries.
  3. Trailing empty text. After the final ; there is an empty piece: 0 drops it (4 elements), -1 keeps it (5 elements).

Frequently Asked Questions

It splits a string around regex matches like split(), but also returns the matched delimiters as separate elements.
Java 21. It is not available in Java 17 or earlier.
A positive limit n applies the pattern at most n - 1 times. 0 applies it everywhere and drops trailing empty strings; a negative value keeps them.
Use a Matcher loop with find(), adding the text before each match and the match itself to a list.
Yes. Escape metacharacters such as . or | with a backslash, or use Pattern.quote().

Next: startsWith()

Check whether a string starts with a prefix, optionally at an offset.

startsWith() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful