Discussions

java - Split string into several two character strings - Stack Overflow
I have this string 8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C How can I split this string into substrings that consist of 2 characters per substring? I'm confused because I cannot find a delimiter to split them. More on stackoverflow.com
🌐 stackoverflow.com
November 9, 2011
regex - Splitting string in between two characters in Java - Stack Overflow
I am currently attempting to interpret some code I wrote for something. The information I would like to split looks something like this: {hey=yes}TEST What I am trying to accomplish, is splitting More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
How do I split a string in Java? - Stack Overflow
I want to split a string using a delimiter, for example split "004-034556" into two separate strings by the delimiter "-": ... That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. More on stackoverflow.com
🌐 stackoverflow.com
Split a string with multiple delimiters

Erm, you can just put all of the delimiters you want in the quotes for the string passed to .split()

[PS] C:\Users\> $string = "part1.part2;part3"
[PS] C:\Users\> $string.split(".;")
part1
part2
part3
More on reddit.com
🌐 r/PowerShell
7
19
October 14, 2014
🌐
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.
🌐
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 - The Java String split() method breaks a string around every match of a regular expression and returns the parts as a String array, and the overload split(regex, limit) also controls how many parts we get and whether trailing empty strings stay ...
🌐
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];
🌐
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. Instead of this, we can use a regex character set This refers to putting the special characters to be escaped inside square brackets. This way, the special characters are treated as normal characters. For example, we could use a | as a delimiter by saying:
🌐
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 ... } ... In the example above, we use : as the delimiter, but note that the parameter the split() method takes is a regular expression....
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - We can achieve the same result ... .toArray(String[]::new); We can split a string into two parts by getting its midpoint and using substring() to get the separated parts....
🌐
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 - A character set is defined by wrapping the desired delimiters in square brackets [ ]. A regex character set like [,;|] tells the Java split() method to treat each character inside the brackets as an individual valid delimiter.
🌐
YouTube
youtube.com › watch
How to split a Java String - 052 - YouTube
In this tutorial we're going to look at how to split a string in Java. We can split the string by character or split the string by words. We'll look at both!...
Published: December 12, 2016
🌐
Baeldung
baeldung.com › home › java › java string › splitting a java string by multiple delimiters
Splitting a Java String by Multiple Delimiters | Baeldung
August 7, 2025 - We just have to define an input string we want to split and a pattern. The next step is to apply a pattern. A pattern can match zero or multiple times. To split by different delimiters, we should just set all the characters in the pattern.
🌐
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.
🌐
iO Flood
ioflood.com › blog › java-string-split
How to Split Strings in Java: Methods and Examples
February 20, 2024 - In this example, even though we split the string, the original string ‘Hello, World!’ remains unchanged. A regular expression, often shortened to ‘regex’, is a sequence of characters that forms a search pattern. This pattern can be used to match, locate, and manage text. Java’s split() method uses regular expressions to determine where to split a string.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-split-method-example
Java String split() Method with examples
Java String split method is used for splitting a String into substrings based on the given delimiter or regular expression. ... We have two variants of split() method in String class.
🌐
IONOS
ionos.com › digital guide › websites › web development › string splitting in java
How to split strings in Java - IONOS
January 6, 2025 - You can split strings in Java using the split() method. We explain how the method works and show a number of examples.
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
🌐
Medium
medium.com › @AlexanderObregon › splitting-a-word-into-two-parts-in-java-strings-c9d1aff469ac
Splitting a Word into Two Parts in Java Strings | Medium
August 22, 2025 - The substring method is the central tool for cutting a word into parts. It works by creating a new string from a specified range of characters, without altering the original. If a word is "Computer", choosing index 4 produces "Comp" as the first ...
🌐
CodeGym
codegym.cc › java blog › strings in java › java string split() method
Java String split method
December 5, 2024 - In Java, the split method splits a string into substrings using a delimiter defined using a regular expression. Let's present the method signature and begin our dive: String[] split(String regex) Two things are clear from the signature: