๐ŸŒ
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 - In Java, we can use the split() method from the String class to split a string by character, which simplifies this process by taking a regular expression (regex) as its argument.
๐ŸŒ
W3Docs
w3docs.com โ€บ java
How to Split a String in Java | Practice with examples
Learn the ways to split a string in Java. The most common way is using the String.split () method, also see how to use StringTokenizer and Pattern.compile ().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ split-string-java-examples
Java String split() Method - GeeksforGeeks
December 20, 2025 - split() method in Java is used to divide a string into an array of substrings based on a specified delimiter or regular expression.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ string โ€บ split
Java String split()
If you need to use special characters such as: \, |, ^, *, + etc, you need to escape these characters. For example, we need to use \\+ to split at +. // 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+e+f";
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-string-split-method-explained-77bdaddaae79
Javaโ€™s String split() Method Explained
June 25, 2024 - The split() method in Java is defined in the String class and comes in two main forms: ... regex: This parameter represents the regular expression used to match the delimiter. A regular expression (regex) is a sequence of characters that forms a search pattern, which can be used for pattern matching within strings.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-string-split
Java String split() method - javatpoint
Java String split() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string split in java etc.
Top answer
1 of 16
3415

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
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ java string split() method โ€“ how to split a string in java
Java String Split() Method โ€“ How To Split A String In Java
April 1, 2025 - This tutorial explains how to split a String using Java String Split() Method. You will learn to use this method to manipulate the String.
Find elsewhere
๐ŸŒ
W3Resource
w3resource.com โ€บ java-tutorial โ€บ string โ€บ string_split.php
Java String: split Method - w3resource
Return Value: the array of strings computed by splitting this string around matches of the given regular expression. ... Throws: PatternSyntaxException - if the regular expression's syntax is invalid. Pictorial presentation of Java String split() Method
๐ŸŒ
BeginnersBook -
beginnersbook.com โ€บ home โ€บ java โ€บ java string split() method with examples
Java String split() Method with examples
December 1, 2024 - 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 ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to split a string in java
How to split a string in Java | Sentry
public class Main { public static ... regular expression. This means it is easy to do things like split on one or more : characters by using :+....
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to split a string in java
How to split a string in Java - Mkyong.com
February 9, 2022 - str.split("\\|") // backslash \ to escape regex special character str.split("[|]") // character class [] to escape regex special character str.split(Pattern.quote("|")) // Pattern#quote() to escape regex special character ยท The below example tries to split a string by a unescape pipe symbol |. ... package com.mkyong.string.split; public class StringSplitSpecial { public static void main(String[] args) { String csv = "a|b|c|d"; String[] output = csv.split("|"); for (String s : output) { System.out.println(s); } } } ... The below example tries to split a string by an escaped pipe symbol |. ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java string.split()
Java.String.split() | Baeldung
March 12, 2025 - In this tutorial, weโ€™ll learn about the String.split() method in Java. This method helps us break a string into smaller pieces, based on a specified delimiter. A delimiter is a character or a sequence of characters that mark the boundaries ...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - String[] result2 = words.split(",",0); // Split on each comma, but discard trailing empty strings. System.out.println("Limit of -2 produces an array of length " + result1.length); System.out.println("Limit of 0 produces an array of length " ...
๐ŸŒ
Briebug Blog
blog.briebug.com โ€บ blog โ€บ java-split-string
Using the Java String.split() Method
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-separate-the-individual-characters-from-a-string
Java Program to Separate the Individual Characters from a String - GeeksforGeeks
July 23, 2025 - Next, create a for-loop where the loop variable will start from index 0 and end at the length of the given string. Print the character present at every index in order to separate each individual character.
๐ŸŒ
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 substrings. There are also some particularities to note when using the method.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 497299 โ€บ java โ€บ Splitting-String-characters
Splitting the String to get all characters (Java in General forum at Coderanch)
"AMIT" to get a String array that would contain all the characters (A,M,I,T). Alternatively I can use the toCharArray() to get all the characters, but work with the Strings instead of characters.