🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
The split() method splits a string into an array of substrings using a regular expression as the separator. If a limit is specified, the returned array will not be longer than the limit.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-split-a-string-by-character
Java - Split a String by Character - GeeksforGeeks
July 23, 2025 - The parts are printed one by one. For more control over the splitting process, we can use StringBuilder to manually build parts of the string. ... // Java program to split a string // using StringBuilder public class SplitString { public static void main(String[] args) { String s = "Java,C,Python"; StringBuilder sb = new StringBuilder(); for (char ch : s.toCharArray()) { // Split by character ',' if (ch == ',') { System.out.println(sb.toString()); sb.setLength(0); // Reset the builder } else { sb.append(ch); // Append the current character } } // Print the last part System.out.println(sb.toString()); } }
🌐
W3Resource
w3resource.com › java-tutorial › string › string_split.php
Java String: split Method - w3resource
August 19, 2022 - Java String split Method: The split() method is used to split a given string around matches of the given regular expression.
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
Java String Split: Use String.split() With Regex | Sentry
September 23, 2026 - public class Main { public static ... to split a string on a character that has special meaning in regular expressions, you need to escape it....
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - Explanation: regex = "[,\\s\\.]" matches commas, spaces, and dots. The string is split wherever any of these characters appear.
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - In Java, we can use String#split() to split a string. String phone = "012-3456789"; // String#split(string regex) accepts regex as the argument String[] output = phone.split("-"); String part1 = output[0];
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-split-method-example
Java String split() Method with examples
Input: "Just.a.Simple.String"; Output: ["Just", "a", "Simple", "String"] ... The ?! part in this regex is negative assertion, which it works like a not operator in the context of regular expression. The ^ is to match the beginning of the string. Together it matches any character that is not ...
🌐
Programiz
programiz.com › java-programming › library › string › split
Java String split() | Programiz
March 12, 2024 - The Java String split() method divides the string at the specified separator and returns an array of substrings. In this tutorial, you will learn about the Java split() method with the help of examples.
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
July 15, 2026 - For example, if we want to split on a dot (.), we need to escape it like this: \\. because the dot (.) is a special character in regular expressions: @Test void whenSplitWithDotDelimiter_thenGetExpectedArray() { String s = "www.example.com"; String[] expected = new String[] { "www", "example", "com" }; String[] result = s.split("\\."); assertArrayEquals(expected, result); } Once data is in a Java String, it’s already decoded into a sequence of Unicode characters.
Find elsewhere
Top answer
1 of 16
3416

Use the appropriately named method String#split().

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

Note that split's argument is assumed to be a regular expression, so remember to escape special characters if necessary.

there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

For instance, to split on a period/dot . (which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.

To test beforehand if the string contains certain character(s), just use String#contains().

if (string.contains("-")) {
    // Split it.
} else {
    throw new IllegalArgumentException("String " + string + " does not contain -");
}

Note, this does not take a regular expression. For that, use String#matches() instead.

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split() method.

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42
2 of 16
91

An alternative to processing the string directly would be to use a regular expression with capturing groups. This has the advantage that it makes it straightforward to imply more sophisticated constraints on the input. For example, the following splits the string into two parts, and ensures that both consist only of digits:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class SplitExample
{
    private static Pattern twopart = Pattern.compile("(\\d+)-(\\d+)");

    public static void checkString(String s)
    {
        Matcher m = twopart.matcher(s);
        if (m.matches()) {
            System.out.println(s + " matches; first part is " + m.group(1) +
                               ", second part is " + m.group(2) + ".");
        } else {
            System.out.println(s + " does not match.");
        }
    }

    public static void main(String[] args) {
        checkString("123-4567");
        checkString("foo-bar");
        checkString("123-");
        checkString("-4567");
        checkString("123-4567-890");
    }
}

As the pattern is fixed in this instance, it can be compiled in advance and stored as a static member (initialised at class load time in the example). The regular expression is:

(\d+)-(\d+)

The parentheses denote the capturing groups; the string that matched that part of the regexp can be accessed by the Match.group() method, as shown. The \d matches and single decimal digit, and the + means "match one or more of the previous expression). The - has no special meaning, so just matches that character in the input. Note that you need to double-escape the backslashes when writing this as a Java string. Some other examples:

([A-Z]+)-([A-Z]+)          // Each part consists of only capital letters 
([^-]+)-([^-]+)            // Each part consists of characters other than -
([A-Z]{2})-(\d+)           // The first part is exactly two capital letters,
                           // the second consists of digits
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - Splits the string variable at the | character. We use two backlashes here since we need to first escape the Java-meaning of the backlash, so the backslash can be applied to the | character.
🌐
How to do in Java
howtodoinjava.com › home › string › java string split(): limit, regex escaping and empty strings
Java String split() : Splitting by One or Multiple Delimiters
October 12, 2023 - A character class such as [,;] splits on any one of the listed characters, and adding + treats a run of delimiters as one. An alternation with | handles delimiters that are longer than one character, and \\s* around a delimiter removes the spaces ...
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › java string split() method: syntax, delimiters, and examples
Understanding Java String Split() Method | Interview Kickstart
April 1, 2026 - For example, using \\d would split the string at every digit, while \\s would split it at every whitespace character. limit: This integer controls the ‘threshold’ of the split.
🌐
Codefinity
codefinity.com › courses › v2 › 0bfa1111-8677-478f-bcd3-315dff1d7d40 › 421ab035-77e8-4fef-abb9-488c91c2d1f9 › 433c4bac-9f33-4b48-9be0-cdc81423c941
Learn Method split() | String Advanced
The split() method is a String method that takes a regex as a parameter. What is regex? Regex stands for regular expression, which is essentially a character or set of characters that we use to split our string.
🌐
Codecademy
codecademy.com › docs › java › strings › .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - String[] result = words.split("\\s+"); // Splits words on matching sequences of whitespace. ... This example uses the pattern “,\s*“ which matches a comma followed by zero or more whitespace characters, it also uses a limit parameter.
🌐
Scaler
scaler.com › home › topics › split() string method in java with examples
split() String Method in Java with Examples - Scaler Topics
April 11, 2024 - The split in Java is a method defined under the String class, which is used to break a string around the matches of the provided regular expression. We can also say that the split in Java is used to break a string based on the provided string delimiter. The string delimiters are mostly comma ...
🌐
Quora
quora.com › How-do-you-split-a-word-in-Java
How to split a word in Java - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Baeldung
baeldung.com › home › java › java string › split a string every n characters in java
Split a String Every n Characters in Java | Baeldung
January 8, 2024 - Another way to split a String object at every nth character is to use the substring method. Basically, we can loop through the string and call substring to divide it into multiple portions based on the specified n characters:
🌐
Medium
medium.com › @AlexanderObregon › javas-string-split-method-explained-77bdaddaae79
Java’s String split() Method Explained
June 25, 2024 - Even though the string is empty, the split() method returns an array with one element (the empty string itself). If the delimiter is empty (""), the split() method will return an array of single-character strings.