How to Use split() Method in Java

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). It’s part of the String class, so you can call it directly on any string object. This guide shows you how to use it effectively, including common variations, pitfalls, and practical examples.

What You’ll Need

  • Java Development Kit (JDK) installed (version 8 or later is fine).
  • A text editor or IDE (like IntelliJ, Eclipse, or VS Code).
  • Basic understanding of Java syntax and arrays.

Step-by-Step

  1. Understand the syntax. The split() method has two overloads:
    • String[] split(String regex) – splits the string wherever the regex matches.
    • String[] split(String regex, int limit) – splits with a limit controlling how many times the pattern is applied.
  2. Call split() on a string. For example, to split a comma-separated list:
    String data = "apple,banana,cherry";
    String[] fruits = data.split(",");
    // fruits = {"apple", "banana", "cherry"}
  3. Use the resulting array. The method returns an array of strings. You can iterate over it with a for-each loop or access elements by index.
    for (String fruit : fruits) {
        System.out.println(fruit);
    }
  4. Handle empty strings. By default, trailing empty strings are removed. For example, "a,b,".split(",") returns {"a", "b"} (the trailing empty string is dropped). If you need to keep them, use a negative limit:
    String[] parts = "a,b,".split(",", -1);
    // parts = {"a", "b", ""}
  5. Limit the number of splits. The limit parameter controls how many times the regex is applied:
    • Positive limit n: the pattern is applied at most n-1 times, and the last element contains the remainder.
    • Negative limit: pattern is applied as many times as possible, and trailing empty strings are kept.
    • Zero (default): pattern applied as many times as possible, but trailing empty strings are removed.
    String s = "one:two:three";
    String[] parts = s.split(":", 2);
    // parts = {"one", "two:three"}
  6. Use regex for complex delimiters. The delimiter is a regular expression, not a literal string. For example, to split on one or more spaces:
    String sentence = "Java   is   fun";
    String[] words = sentence.split("\\s+");
    // words = {"Java", "is", "fun"}

    Note the double backslash to escape the backslash in a Java string literal.

  7. Escape special characters. If your delimiter is a dot (.), pipe (|), or other regex metacharacter, you must escape it with \\ or use Pattern.quote():
    String ip = "192.168.1.1";
    String[] octets = ip.split("\\."); // escape the dot
    // or
    String[] octets2 = ip.split(Pattern.quote("."));
  8. Split on multiple delimiters. Use a character class or alternation. Example: split on comma or semicolon:
    String csv = "a;b,c";
    String[] items = csv.split("[;,]");
    // items = {"a", "b", "c"}
  9. Check for null or empty input. If the string is null, calling split() will throw a NullPointerException. If it’s empty, you get an array with one empty string (unless you use a negative limit).
    String empty = "";
    String[] arr = empty.split(","); // arr = {""}

Common Mistakes

  • Forgetting to escape regex metacharacters. Splitting on a dot (".") without escaping will split on any character, not the literal dot.
  • Assuming split() modifies the original string. Strings are immutable; split() returns a new array and leaves the original string unchanged.
  • Ignoring trailing empty strings. When the string ends with the delimiter, the default behavior removes the final empty string, which may cause off-by-one errors.
  • Using split() with a regex that matches an empty string. For example, "abc".split("") splits between every character, returning {"a", "b", "c"} (in Java 8 and later, the leading empty string is removed). This can be surprising.
  • Not handling null input. Always check for null before calling split() to avoid exceptions.

Tips

  • Use Pattern.quote() when your delimiter is a literal string that may contain regex special characters. It’s cleaner than escaping manually.
  • For performance, if you split the same pattern many times, precompile the Pattern object and use pattern.split() instead of String.split().
  • To split a string into individual characters, use toCharArray() instead of split("") – it’s more efficient and clearer.
  • Remember that split() returns an array; if you need a List, wrap it with Arrays.asList().
  • When using limit, a positive limit can be useful to parse only the first few fields and keep the rest intact.

Troubleshooting

  • My split isn’t working as expected. Check if your delimiter contains regex metacharacters. Escape them or use Pattern.quote(). Also, verify the exact string content – hidden characters like tabs or newlines may require \t or \n in the regex.
  • I get an empty array or missing elements. This often happens with trailing delimiters. Use a negative limit (-1) to keep trailing empty strings, or adjust your input.
  • I get a PatternSyntaxException. Your regex is invalid. For example, an unclosed bracket or an invalid escape. Review the regex and test it separately.
  • I get a NullPointerException. The string variable is null. Add a null check before calling split().
  • The split result has an extra empty string at the beginning. This can happen when the string starts with the delimiter. For example, ",a".split(",") returns {"", "a"}. If you don’t want that, strip leading delimiters or handle it in your logic.

Frequently Asked Questions

  • Can I split a string by a newline character? Yes, use split("\n") for LF newlines, but on Windows you may have \r\n. Use split("\r?\n") to handle both.
  • What’s the difference between split(",") and split(",", -1)? The default (zero limit) removes trailing empty strings. A negative limit keeps all trailing empty strings, so the array length reflects every delimiter occurrence.
  • Does split() change the original string? No, strings are immutable. It returns a new array of substrings.
  • Can I split on multiple characters at once? Yes, use a regex character class like [;,] or alternation like ;|,.
  • Why does "a,b,".split(",") return only two elements? Because trailing empty strings are removed by default. Use split(",", -1) to get three elements including the empty string.
  • Is split() thread-safe? Yes, since String objects are immutable and the method doesn’t modify any state, it’s safe to use from multiple threads.

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.