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
🌐
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. The last element of the array will contain the remainder of the string, which may still have separators in it if the limit was reached. Tip: See the Java ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - The split() method in Java is used to divide a string into multiple parts based on a specified delimiter (regular expression). It returns an array of substrings after splitting the original string.
Discussions

Could somebody help me with using the .split() method?
Alright, so you want to use split(). So how does that work? By looking at the javadoc we have String[] split(String regex) Splits this string around matches of the given regular expression. What does that mean? It means that we can split a string into an array of strings with the appropriate regular expression. So what would "Hello World".split(" ") give? From that, you might construct a loop that applies the effect of compute to each word individually. As a note, while I understand that sometimes people can be honestly stumped by fairly basic things, you should try and check javadoc first, it often has examples. More on reddit.com
🌐 r/java
23
1
January 2, 2014
[Java] Simple question about String split()
Is it null? No. Empty strings (""). More on reddit.com
🌐 r/learnprogramming
10
1
March 28, 2016
Split String every nth char / or the first occurrence of a period
If you want to split only on full sentences, just check for the location of the next full stop. If the phrase length is less than 15 characters, search for the subsequent full stop, check the length of both is still less than 15, and add it to the phrase. Repeat until the phrase is longer than 15 characters. More on reddit.com
🌐 r/java
8
0
August 28, 2013
split comma separated list, and strip spaces.

howdy stib,

the simplest solution is to use .Trim() on the results of your split. that will trim away any leading/trailing spaces. [grin] something like this ...

'foo, foo bar, fuzz, baz'.Split(',').Trim()   

take care,
lee

More on reddit.com
🌐 r/PowerShell
17
6
August 19, 2019
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
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-split-method-example
Java String split() Method with examples
2. String[] split(String regex, ... the number of strings returned after split up. For example: split("anydelimiter", 3) would return the array of only 3 strings even if there can be more than three substrings....
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
How to split a string in Java | Sentry
If the split character is not present ... string. public class Main { public static void main(String[] arg) { String str = "how.to.split.a.string.in.java"; String[] arrOfStr = str.split("z"); for (String a : arrOfStr) { System.out.println(a); } } }...
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
July 15, 2026 - A delimiter is a character or a sequence of characters that mark the boundaries between the pieces. For example, we might want to split a sentence into words or a list of values separated by commas.
🌐
Medium
medium.com › @AlexanderObregon › javas-string-split-method-explained-77bdaddaae79
Java’s String split() Method Explained
June 25, 2024 - In this example, the string "Hello world from Java" is split into an array of substrings using the space " " as the delimiter.
Find elsewhere
🌐
IONOS
ionos.com › digital guide › websites › web development › string splitting in java
How to split strings in Java - IONOS
January 6, 2025 - In this example, a string variable x is ini­tial­ized. Then Java’s split() is used on the string. The parameter searches the string for spaces and splits it in the places where it finds them. The result is then saved in an array called “output”. The for loop is used to list the sub­strings.
🌐
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.
🌐
How to do in Java
howtodoinjava.com › home › string › java string split() : splitting by one or multiple delimiters
Java String split() : Splitting by One or Multiple Delimiters
October 12, 2023 - String str = "how-to-do.in.java"; String[] strArray1 = str.split("-"); //[how, to, do.in.java] - 3 tokens String[] strArray2 = str.split("-|\\."); //[how, to, do, in, java] - 5 tokens
🌐
Briebug
blog.briebug.com › home › articles › using the java string.split() method
Using the Java String.split() Method | Briebug
October 13, 2022 - The Java String.split() method has two variations, commonly known as method overloading, which are both used to split a String into an array of Strings, using a passed delimiter or regular expression.
🌐
Programiz
programiz.com › java-programming › library › string › split
Java String split()
// importing Arrays to convert array to string // used for printing arrays import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a::b::c::d:e"; // splitting the string at "::" // storing the result in an array of strings
🌐
Guru99
guru99.com › home › java tutorials › split() string method in java: how to split string with example
Split() String Method in Java: How to Split String with Example
December 26, 2023 - This tutorial covers the split() string method in java definitions, How to split string in java with a delimiter, Split string with regex and length, and split with space.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › split in java
Java String split() Method Explained with Examples and Use Cases
May 30, 2025 - The split(String regex) method is a part of the Java String split functionality. It splits the string into an array of substrings based on the specified regular expression. It splits the original string wherever the regex matches and returns all resulting parts as separate elements in the array. This example splits a string using a colon : as the delimiter.
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - String[] splitted = input.trim().split("\\s*,\\s*"); Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter.
🌐
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 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 to add a limit that can be negative, positive, or equal to 0.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string split() method
Java String split method
December 5, 2024 - String[] split(String regex) Two things are clear from the signature: The method returns an array of strings. The method has a string input parameter called regex. Let us analyze each of these separately as we break down the description given above. The method returns an array of strings. The declaration contains the following words: "In Java, the split method splits a string into substrings."
🌐
xperti
xperti.io › home › split() string method in java with example
Split() String Method In Java With Example
August 3, 2022 - This version of the Java string split method has a single parameter. It just takes a regular expression as a parameter and splits the given string from the points that match the regular expression. It returns an array of substrings containing the split parts of the original string. It can throw the PatternSyntaxException if the syntax of the passed regular expression would be invalid. See this example below demonstrating scenarios with different number of occurrences of patterns:
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_split.htm
Java - String split() Method
It returns the array of strings ...torialspoint.com"); System.out.println("Return Value :" ); for (String retval: Str.split("-")) { System.out.println(retval); } } }...