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
Answer from BalusC on Stack Overflow
🌐
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];
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › test › jdk › java › lang › String › Split.java
openjdk-jdk11/test/jdk/java/lang/String/Split.java at master · AdoptOpenJDK/openjdk-jdk11
March 2, 2019 - throw new RuntimeException("String.split failure 11"); } int off = r.nextInt(source.length()); String[] srcStrs = new String[] { "", source, regex + source, source + regex, source.substring(0, 3) + regex + source.substring(3, 9) + regex + source.substring(9, 15) + regex + source.substring(15), source.substring(0, off) + regex + source.substring(off)
Author: AdoptOpenJDK
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
July 15, 2026 - We can also pass a limit on the number of splits to the split() method. The limit determines how many times the string will be split: If the limit is greater than 0, the string is split at most limit – 1 times.
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
🌐
How to do in Java
howtodoinjava.com › home › string › split a string in java: split(), pattern, scanner and more
Split a String in Java: split(), Pattern, Scanner and More
3 weeks ago - To split a string in Java, we call String.split() with a delimiter regex, which returns a String[] of the parts between the matches.
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - Explanation: In this example, a string "Geeks for Geeks" is created and split using the split(" ") method, where a space is used as the delimiter. This divides the string into individual words and stores them in an array arr.
Find elsewhere
🌐
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.
🌐
Javaprogramto
javaprogramto.com › 2020 › 07 › java-string-split-examples-on-how-to-split-a-string.html
Java String split() Examples on How To Split a String With Different Delimiters
December 6, 2020 - Splitting string with dot using regex 1 Users javaprogramto Documents course blog workspace CoreJava split.java No of folders in the path : 8 · You can use the whitespace regex: str = "Hello baby, welcome to sweet world"; String[] splited = str.
🌐
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 next to it in the same step. String tags = "java, spring;docker kafka"; String[] anyOf = tags.split("[,;\\s]+"); // [java, spring, docker, kafka] String[] trimmed = "a , b ;c".split("\\s*[,;]\\s*"); // [a, b, c] String[] words = "one AND two OR three".split("\\s+(AND|OR)\\s+"); // [one, two, three]
🌐
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 void main(String[] arg) { String str = "how:to:split:a:string:in:java"; String[] arrOfStr = str.split(":"); for (String a : arrOfStr) { System.out.println(a); } } }
🌐
IONOS
ionos.com › digital guide › websites › web development › string splitting in java
How to split strings in Java - IONOS
January 6, 2025 - The method split() can be used to split strings in Java. It contains a parameter for the separator and an optional parameter for the number of sub­strings. There are also some par­tic­u­lar­i­ties to note when using the method.
🌐
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 - While StringTokenizer is faster ... split method is a built-in function of the java.lang.The String class is used to decompose a string into an array of substrings....
🌐
DataFlair
data-flair.training › blogs › java-string-split-method
Java String split() Method - DataFlair
November 18, 2024 - The split() method in Java splits a String into an array of substrings based on a delimiter.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-split-method-explained-77bdaddaae79
Java’s String split() Method Explained
June 25, 2024 - The split() method allows you to divide a string into an array of substrings based on a specified delimiter, which can be useful in various scenarios such as parsing data, manipulating text, and more.
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_split.htm
Java - String split() Method
It returns the array of strings computed by splitting this string around matches of the given regular expression. import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome-to-Tutorialspoint.com"); System.out.println("Return Value :" ); for (String retval: Str.split("-")) { System.out.println(retval); } } }
🌐
Dreamix
dreamix.eu › home › insights › tech › splitting string in java – examples and tips
Splitting String in Java - Examples and Tips - Dreamix
July 16, 2024 - The method is simply splitting the string by using a delimiter passed as a parameter. The parameter can be a regular expression or a simple character. Method signature can be: ... String[] split​(String regex, int limit) There is also an option ...
🌐
Java67
java67.com › 2013 › 03 › how-to-split-string-in-java-regular-expression.html
How to Split String in Java using Regular Expression | Java67
... String class provides split() ... space or any arbitrary method. split() method splits the string based on delimiter provided, and return a String array, which contains individual Strings....
🌐
Vultr Docs
docs.vultr.com › java › standard library › java › lang › string › split()
Java String split()
December 17, 2024 - The Java split() method is a part of the String class in Java, used extensively for dividing a string into multiple parts based on a specified delimiter.