Split in Java String: Methods, Examples, and Pitfalls

How-to basics · Updated November 23, 2026

The split() method in Java splits a string into an array of substrings based on a delimiter (regular expression). The simplest usage is str.split(",") which returns a String[] containing the parts separated by commas. This article explains how split() works, its overloads, common pitfalls, and practical examples.

Understanding the split() Method

String.split(String regex) is a built-in method in the java.lang.String class. It takes a regular expression (regex) as the delimiter and returns an array of strings. The method internally uses Pattern.compile(regex).split(this, limit), so the delimiter is always treated as a regex, not a literal string. There are two overloads:

  • split(String regex) – splits with no limit, trailing empty strings are removed.
  • split(String regex, int limit) – controls how many times the pattern is applied and whether trailing empty strings are kept.

For example, "a,b,c".split(",") returns {"a", "b", "c"}. The regex can be more complex, like "\\s+" for whitespace.

How split() Works Internally

When you call split(), Java compiles the regex into a Pattern object (unless it’s a one-character regex with no metacharacters, which uses a fast path). The method then scans the input string, finds matches of the regex, and creates substrings between matches. The limit parameter affects the result:

  • limit > 0: The pattern is applied at most limit-1 times, and the array length will not exceed limit. The last element contains the remainder of the string, including any delimiters.
  • limit < 0: The pattern is applied as many times as possible, and trailing empty strings are kept.
  • limit == 0: The pattern is applied as many times as possible, but trailing empty strings are discarded.

For instance, "a,,b".split(",", -1) returns {"a", "", "b"}, while "a,,b".split(",") returns {"a", "", "b"} because the empty string is in the middle, not trailing.

Choosing the Right Delimiter

Because the delimiter is a regex, special characters like dot (.), pipe (|), and backslash (\\) need escaping. A common mistake is using split(".") to split on a literal dot; this actually matches any character, resulting in an array of empty strings. To split on a literal dot, use split("\\.") (double backslash in a string literal). Similarly, to split on a pipe, use split("\\|"). For a backslash, use split("\\\\") (four backslashes in the string literal).

If you need to split on a literal string that contains many regex metacharacters, consider using Pattern.quote() to escape the entire string: str.split(Pattern.quote("a.b")).

Common Pitfalls and Misconceptions

One frequent issue is the removal of trailing empty strings. For example, "a,b,".split(",") returns {"a", "b"}, not {"a", "b", ""}. To keep the trailing empty string, use split(",", -1). Another pitfall is that split() returns an array, not a list; if you need a List, you must convert manually, e.g., Arrays.asList(str.split(",")).

Also, the regex is applied to the entire string, so a delimiter like "\\d" splits on any digit. If you want to split on a literal sequence like “123”, you need to escape it or use Pattern.quote.

Performance Considerations

For simple one-character delimiters that are not regex metacharacters (like a comma or a space), Java uses an optimized path that is faster than compiling a full regex. However, for complex regexes, the Pattern is compiled each time you call split() unless you reuse a Pattern object. If you split many strings with the same regex, precompile the pattern:

Pattern p = Pattern.compile(",");
String[] parts = p.split(str);

This avoids recompiling the regex and improves performance.

Alternatives to split()

Depending on your needs, other methods might be more appropriate:

  • StringTokenizer (legacy) – does not support regex, but is slightly faster for simple delimiters. However, it is not recommended for new code.
  • Scanner – can parse tokens with a custom delimiter, useful for reading from streams.
  • String.substring() and indexOf() – for simple splitting without regex, you can manually loop, but this is more error-prone.
  • Java 8+ Streams – you can use Arrays.stream(str.split(",")) to process the parts functionally.

For example, to split and trim whitespace, you might use Arrays.stream(str.split(",")).map(String::trim).toArray(String[]::new).

Frequently Asked Questions

How do I split a string by multiple delimiters?

Use a regex character class. For example, to split by comma, semicolon, or pipe, use str.split("[,;|]"). The regex matches any of the characters inside the brackets.

Why does split() return an empty array for an empty string?

If the input string is empty (""), split() returns an array containing one empty string: {""}. This is because the regex finds no matches, and the entire string is returned as the only element. If you want an empty array, you can check str.isEmpty() first.

How do I split a string and keep the delimiters?

Java’s split() does not keep delimiters. To retain them, you can use a regex with lookahead or lookbehind. For example, to split on commas and keep them, use str.split("(?<=,)|(?=,)"). This splits before and after each comma, so the comma stays attached to the preceding or following part.

What is the difference between split() and StringTokenizer?

StringTokenizer is a legacy class that does not support regular expressions and treats delimiters as single characters. split() is more powerful and recommended. Also, StringTokenizer can return the delimiters as tokens if you pass a flag, but it is generally slower and less flexible.

Can split() throw a PatternSyntaxException?

Yes, if the regex is invalid, split() throws an unchecked PatternSyntaxException. For example, "a".split("[") throws because [ is an unclosed character class. Always ensure your regex is valid or catch the exception.

How do I split a string into individual characters?

You can use str.split("") but this returns leading and trailing empty strings in some Java versions. A better way is to use str.toCharArray() or iterate with str.charAt(i). If you need a String[], use str.split("(?<=\\.)") but that's overkill; simply use str.toCharArray().

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.