Java example

How to split a string in Java

8 min read Updated Sep 2026 Runs in an isolated runtime
Quick answer

Use s.split(","), which returns a String[]: "java,kotlin,scala".split(",") gives [java, kotlin, scala]. The separator is a regular expression, so escape ., | and $ (split("\\.")) or wrap it in Pattern.quote(sep). For a List, wrap the result: Arrays.asList(s.split(",")).

String.split is the answer, and it has three behaviours that surprise almost everyone the first time. The argument is a regex, not plain text, so split(".") returns an empty array. Empty strings at the end of the result are silently removed unless you pass a limit. And splitting on a single space falls apart the moment the input has two spaces or a tab. This page covers all three with real output, then turns the pieces into a List, compares the legacy StringTokenizer, and ends with a table. Each example runs on this page: hit Run, then edit the code and run it again.

1String.split()Recommended

s.split(regex) cuts the string at every match of the separator and returns the pieces as a String[]. The separator can be more than one character, and the original string is untouched, because Java strings are immutable. Print the array with Arrays.toString; printing the array directly shows its type and a hash, not the contents (see printing an array).

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String csv = "java,kotlin,scala";
        String[] parts = csv.split(",");
        System.out.println(Arrays.toString(parts) + " " + parts.length);
        System.out.println(parts[0] + " " + parts[2]);

        // The separator can be longer than one character
        System.out.println(Arrays.toString("a -> b -> c".split(" -> ")));

        // Separator not found: one element, the whole input
        String[] missing = "nope".split(",");
        System.out.println(Arrays.toString(missing) + " " + missing.length);

        // Empty input: an array holding one EMPTY string, not an empty array
        String[] empty = "".split(",");
        System.out.println(Arrays.toString(empty) + " " + empty.length);
        System.out.println(empty[0].isEmpty());
    }
}

Output

Prints [java, kotlin, scala] 3 and java scala, then [a, b, c] for the multi-character separator. When the separator isn't found you get the whole input back as one element, [nope] 1. The empty-input line is the one to remember: [] 1. It looks empty, but the array holds one empty string (the last line prints true for empty[0].isEmpty()), so parts.length == 0 does not mean "no input". Check s.isEmpty() before splitting instead.

2The separator is a regex: escaping ., | and $

The parameter of split is named regex for a reason. A comma or a semicolon means itself, but . means "any character", | means "or" and $ means "end of input", and each one produces a silently wrong result instead of an error. Escape the character with a backslash (written \\ in a Java string literal), put it in a character class like [.], or let Pattern.quote do the escaping for you.

Main.java
import java.util.Arrays;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String version = "25.0.3";

        // "." is regex for "any character": every character is a separator,
        // every piece is empty, and trailing empties are dropped
        System.out.println(version.split(".").length);

        // Escape it (the backslash itself is escaped in Java source)...
        System.out.println(Arrays.toString(version.split("\\.")));
        // ...or put it in a character class
        System.out.println(Arrays.toString(version.split("[.]")));

        // "|" means "or": empty OR empty, which matches between every character
        System.out.println(Arrays.toString("red|green".split("|")));
        System.out.println(Arrays.toString("red|green".split("\\|")));

        // "$" means end of input, so it never matches inside the string
        System.out.println(Arrays.toString("10$20$30".split("$")));

        // Pattern.quote makes any separator literal: use it for variables
        String sep = "$";
        System.out.println(Pattern.quote(sep));
        System.out.println(Arrays.toString("10$20$30".split(Pattern.quote(sep))));

        // A character class splits on several delimiters at once
        System.out.println(Arrays.toString("a;b,c|d".split("[;,|]")));
    }
}

Output

"25.0.3".split(".") has length 0: every character matched as a separator, every piece between them was empty, and trailing empties are dropped (section 3), which here means all of them. Escaped or in a class, it prints [25, 0, 3]. An unescaped pipe splits between every character, [r, e, d, |, g, r, e, e, n], and "$" never matches inside the string, so you get [10$20$30] back unchanged. Pattern.quote("$") returns \Q$\E, regex syntax for "everything between is literal", and splits correctly into [10, 20, 30]. The last line shows the useful side of regex: [;,|] splits on any of three delimiters at once, giving [a, b, c, d]. Splitting on a single ordinary character such as , skips the regex engine entirely, so there is no speed penalty for the common case.

3Trailing empty strings and the limit argument

One-argument split(regex) is split(regex, 0), and a limit of 0 removes empty strings from the end of the result. Empty strings at the start and in the middle stay. The second argument changes that: a negative limit keeps everything, and a positive limit n stops after n pieces, leaving the rest of the string, separators and all, in the last one. This snippet prints each piece in quotes so the empty ones are visible.

Main.java
import java.util.Arrays;
import java.util.stream.Collectors;

public class Main {
    // Quote each piece so empty strings are visible, then print the length
    static String show(String[] parts) {
        return Arrays.stream(parts)
                .map(p -> "\"" + p + "\"")
                .collect(Collectors.joining(", ", "[", "] ")) + parts.length;
    }

    public static void main(String[] args) {
        String row = "ada,,london,,";

        // Default (limit 0): empties in the middle stay, trailing ones are dropped
        System.out.println(show(row.split(",")));

        // limit -1 (any negative number): keep every piece, trailing empties too
        System.out.println(show(row.split(",", -1)));

        // Leading empties are never dropped
        System.out.println(show(",a,b".split(",")));

        // Nothing but separators: every piece is trailing, so the array is empty
        System.out.println(show(",,,".split(",")));

        // limit n > 0: at most n pieces, and the last one holds the rest unsplit
        System.out.println(show("a,b,c,d".split(",", 3)));

        // Split once with limit 2: an "=" inside the value survives
        String setting = "url=https://x.io/?q=1";
        String[] kv = setting.split("=", 2);
        System.out.println(kv[0] + " -> " + kv[1]);
        System.out.println(show(setting.split("=")));
    }
}

Output

With the default limit, "ada,,london,," gives ["ada", "", "london"] 3: the middle empty survives, the two trailing ones are gone. With -1 all five pieces come back. A leading empty is always kept (["", "a", "b"] 3), and a string of nothing but commas gives [] 0, because every piece is a trailing one. For rows of fixed columns, always pass -1, or a blank last column quietly shortens the array and parts[4] throws ArrayIndexOutOfBoundsException. A limit of 2 is how you split exactly once: url -> https://x.io/?q=1 keeps the = inside the value, where a plain split("=") cuts the URL into three pieces. If the separator is missing, a limit-2 split returns one element, so check kv.length == 2 before reading kv[1].

4Splitting on whitespace: "\\s+" and strip()

Splitting a sentence with split(" ") is the classic bug. Two spaces in a row make an empty piece, a tab isn't a separator at all, and leading spaces add empties at the front. The regex "\\s+" matches any run of whitespace, which fixes the middle, but a leading run still leaves one empty string in front. Call strip() (Java 11+) or trim() first, then split.

Main.java
import java.util.Arrays;
import java.util.stream.Collectors;

public class Main {
    // Quote each piece (showing tabs and newlines) and print the length
    static String show(String[] parts) {
        return Arrays.stream(parts)
                .map(p -> "\"" + p.replace("\t", "\\t").replace("\n", "\\n") + "\"")
                .collect(Collectors.joining(", ", "[", "] ")) + parts.length;
    }

    public static void main(String[] args) {
        String s = "  ship   it\tnow\n";

        // One space as the separator: every extra space is an empty piece,
        // and the tab and the newline are not separators at all
        System.out.println(show(s.split(" ")));

        // \\s+ splits on any run of whitespace, but the leading run
        // still leaves one empty string at the front
        System.out.println(show(s.split("\\s+")));

        // strip() the ends first (Java 11+), then split: just the words
        System.out.println(show(s.strip().split("\\s+")));

        // Blank input still gives one empty string, not zero words
        String blank = "   ";
        System.out.println(show(blank.strip().split("\\s+")));
        String[] words = blank.isBlank() ? new String[0] : blank.strip().split("\\s+");
        System.out.println(show(words));
    }
}

Output

split(" ") returns six pieces, four of them empty, with the tab and newline still inside "it\tnow\n". split("\\s+") gets the words but starts with an empty string, ["", "ship", "it", "now"] 4, and only strip() plus "\\s+" gives the clean ["ship", "it", "now"] 3. There is no trailing empty in the second line because trailing empties are dropped anyway. Blank input is the last trap: it strips to "", which splits to [""] 1, so guard with isBlank() (Java 11+) if zero words should mean an empty array. strip() removes Unicode whitespace, while trim() only removes characters up to U+0020; for ordinary ASCII input they give the same result.

5Into a List: Arrays.asList, List.of and streams

split returns an array, and the second most-asked version of this question is how to get a List instead. Arrays.asList wraps the array without copying it, List.of (Java 9+) makes an unmodifiable copy, and a stream lets you clean each piece on the way. Pattern.splitAsStream skips the array altogether and is the natural choice when you split with the same regex many times, since the pattern is compiled once. For more on the array-to-list step, see converting an array to a list.

Main.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        // Arrays.asList: a fixed-size List backed by the array
        List<String> fixed = Arrays.asList("red,green,blue".split(","));
        System.out.println(fixed);
        try {
            fixed.add("cyan");
        } catch (UnsupportedOperationException e) {
            System.out.println("add failed: " + e.getClass().getSimpleName());
        }

        // Copy into an ArrayList when you need to add or remove
        List<String> growable = new ArrayList<>(Arrays.asList("red,green,blue".split(",")));
        growable.add("cyan");
        System.out.println(growable);

        // List.of (Java 9+): unmodifiable, a safe list to hand out
        List<String> frozen = List.of("red,green,blue".split(","));
        System.out.println(frozen.size() + " " + frozen.get(1));

        // Messy input: trim every piece and drop the blanks with a stream
        String messy = " red, green ,blue,  ,";
        List<String> clean = Arrays.stream(messy.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList(); // Java 16+; use collect(Collectors.toList()) on 8-15
        System.out.println(clean);

        // Or let the regex swallow the spaces around each comma
        System.out.println(Arrays.asList("red , green,blue".split("\\s*,\\s*")));

        // Pattern.splitAsStream (Java 8+): compile once, split straight into a Stream
        Pattern comma = Pattern.compile("\\s*,\\s*");
        List<Integer> ids = comma.splitAsStream("3, 1 ,4,1,5")
                .map(Integer::valueOf)
                .toList();
        System.out.println(ids);
        System.out.println(ids.stream().mapToInt(Integer::intValue).sum());
    }
}

Output

Arrays.asList prints [red, green, blue] but refuses to grow: add failed: UnsupportedOperationException. It is a fixed-size view of the array (set works, add and remove don't), so copy it into an ArrayList when you need to change its size, which gives [red, green, blue, cyan]. The stream turns the messy " red, green ,blue, ," into a clean [red, green, blue], and the "\\s*,\\s*" regex removes the spaces around each comma in one call, though not blank entries or spaces at the very ends. splitAsStream feeds straight into map(Integer::valueOf), giving [3, 1, 4, 1, 5] and a sum of 14. Parsing each piece is covered in converting a String to an int, and the reverse trip in joining a list into a string.

6split() vs StringTokenizer (legacy)

Older code and old answers use java.util.StringTokenizer. It predates regex support in Java, and its documentation calls it "a legacy class that is retained for compatibility reasons although its use is discouraged in new code". It still works, but it differs from split in two ways that matter: its delimiter argument is a set of single characters, not a pattern, and it skips empty tokens without telling you.

Main.java
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        String row = "ada,,london;1815";

        // StringTokenizer: the delimiter string is a SET of characters,
        // and empty tokens are silently skipped
        StringTokenizer st = new StringTokenizer(row, ",;");
        System.out.println(st.countTokens());
        while (st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }

        // split with a character class does the same job but keeps the
        // empty field, so every column stays in its position
        String[] cols = row.split("[,;]");
        System.out.println(Arrays.toString(cols) + " " + cols.length);

        // The no-argument delimiters are space, tab, newline, CR and form feed
        StringTokenizer words = new StringTokenizer("  ship   it\tnow\n");
        System.out.println(words.countTokens());
    }
}

Output

The tokenizer reports 3 tokens, ada, london and 1815, and the blank second field has vanished, so you can no longer tell which column was which. split("[,;]") returns [ada, , london, 1815] 4 with the empty field in place. With no delimiter argument, the tokenizer splits on whitespace and finds 3 words in " ship it\tnow\n", the same as strip().split("\\s+") in section 4. In new code, use split or Pattern.

7Which should you use?

MethodEmpty piecesReturnsBest for
s.split(",")Trailing ones droppedString[]A known separator: the default
s.split(",", -1)All keptString[]Rows with a fixed number of columns
s.split("=", 2)At most 2 piecesString[]key=value, splitting once
s.split(Pattern.quote(sep))Trailing ones droppedString[]A separator in a variable
s.strip().split("\\s+")None (unless blank)String[]Words separated by whitespace
Arrays.stream(s.split(",")).map(String::trim)Filter them outList<String>Comma lists with stray spaces
Pattern.compile(re).splitAsStream(s)Trailing ones droppedStream<String>A reused regex, stream pipelines
new StringTokenizer(s, ",")Always skippedTokens one by oneLegacy code only

Frequently asked questions

How do I split a string on a dot or a pipe in Java?

Escape it, because split takes a regular expression: . means "any character" and | means "or", so "25.0.3".split(".") returns an array of length 0. Write split("\\.") or split("\\|") (two backslashes in Java source), use a character class such as split("[.]"), or wrap the separator with Pattern.quote("."), which is the safe choice when it comes from a variable. A backslash separator needs four in source: "C:\\dev\\tools".split("\\\\") gives [C:, dev, tools].

Why does split() remove the empty strings at the end?

Because split(regex) is split(regex, 0), and a limit of 0 drops trailing empty strings. Empty strings at the start and in the middle are kept, so "ada,,london,,".split(",") has 3 elements. Pass a negative limit to keep every piece: "ada,,london,,".split(",", -1) has 5. Use -1 for CSV-style rows, where a blank last column would otherwise shorten the array.

How do I convert a comma-separated String to a List in Java?

Split it and wrap the array: Arrays.asList(s.split("\\s*,\\s*")), where the regex also swallows spaces around each comma. That list is fixed-size, so add throws UnsupportedOperationException; copy it with new ArrayList<>(...) if you need to change it, or use List.of(s.split(",")) (Java 9+) for an unmodifiable one. To trim each piece and drop blanks, stream it: Arrays.stream(s.split(",")).map(String::trim).filter(x -> !x.isEmpty()).toList() (toList() is Java 16+). Outside plain Java SE, Guava's Splitter does the same job.

How do I split a string into lines in Java?

Use text.lines() (Java 11+), which returns a Stream<String> and understands \n, \r\n and \r, or text.split("\\R") (Java 8+) for an array, since \R is the regex for any line break. Both turn "a\r\nb\nc\n" into [a, b, c]. Splitting on "\n" alone leaves a carriage return on each Windows line, giving [a\r, b, c].

How do I split a string into characters in Java?

"abc".split("") returns [a, b, c] (since Java 8 there is no leading empty string), and s.toCharArray() gives char values. Both work in UTF-16 code units, so an emoji is cut into two halves: "a😀b".split("") has 4 elements. For whole characters use s.codePoints().mapToObj(Character::toString).toList() (Java 16+ for toList()), which gives [a, 😀, b].

Should I use split() or StringTokenizer?

Use split or Pattern in new code. The JDK documentation calls StringTokenizer a legacy class whose use is discouraged. It treats its delimiter string as a set of single characters, cannot take a regex, and silently skips empty tokens: new StringTokenizer("ada,,london;1815", ",;") yields 3 tokens, while "ada,,london;1815".split("[,;]") returns 4 pieces with the blank field in place.

Run it yourself

Open any of these in the full Java editor: tweak, run, and share.

Java playground