Just use:

String[] terms = input.split("[\\s@&.?$+-]+");

You can put a short-hand character class inside a character class (note the \s), and most meta-character loses their meaning inside a character class, except for [, ], -, &, \. However, & is meaningful only when comes in pair &&, and - is treated as literal character if put at the beginning or the end of the character class.

Other languages may have different rules for parsing the pattern, but the rule about - applies for most of the engines.

As @Sean Patrick Floyd mentioned in his answer, the important thing boils down to defining what constitute a word. \w in Java is equivalent to [a-zA-Z0-9_] (English letters upper and lower case, digits and underscore), and therefore, \W consists of all other characters. If you want to consider Unicode letters and digits, you may want to look at Unicode character classes.

Answer from nhahtdh on Stack Overflow
🌐
Quora
quora.com › In-Java-how-do-you-split-strings-with-special-characters
In Java, how do you split strings with special characters? - Quora
Answer (1 of 3): 1)Split using split method in java.lang.String [code]import static java.lang.System.*; public class Main { public static void main(String[] a){ String str = "Test drive in Java~Hello, world!"; String delimeter="~"; String[] strArr=str.split(delimeter); for(Str...
🌐
Medium
medium.com › sina-ahmadi › java-regex-6e4d073aab85
Java RegEx. special characters issue in Java split… | by Sina | My journey as a software developer | Medium
June 20, 2018 - Replace all special characters using Java’s “replaceAll” method in the input string, with escaped special characters ... escapedString = nonEscapedString.replaceAll("\\*", "\\\\*"); splittedString = escapedString.split("\\*")
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - This way, the special characters are treated as normal characters. For example, we could use a | as a delimiter by saying: ... The split() method of the Java String class is a very useful and often used tool. Most data, especially that obtained from reading files would require some amount of ...
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - String phone = "012-345-678-9"; String[] output = phone.split("-", 2); System.out.println(output.length); // 2 String part1 = output[0]; // 012 String part2 = output[1]; // 345-678-9 · This section talks about escaping the special regex characters as a delimiter.
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
How to split a string in Java
September 23, 2026 - public class Main { public static ... a string on a character that has special meaning in regular expressions, you need to escape it....
🌐
Real's HowTo
rgagnon.com › javadetails › java-0438.html
Split a string using String.split() - Real's Java How-to
public class StringSplit { public ....toString(testString.split("\\|"))); // output : [Real, How, To] } } The special character needs to be escaped with a "\" but since "\" is also a special character in Java, you need to escape ...
🌐
YouTube
youtube.com › coding trainer
Java Split on Spaces and Special Characters | Java String Split on Spaces and Special Characters - YouTube
Java Split on Spaces and Special Characters | Java String Split on Spaces and Special CharactersJust use:String[] terms = input.split("[\\s@&.?$+-]+");You ca...
Published: October 31, 2019
Views: 577
Find elsewhere
🌐
TutorialKart
tutorialkart.com › java › how-to-split-a-string-with-specific-character-as-delimiter-in-java
How to Split a String with Specific Character as Delimiter in Java?
January 13, 2021 - To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
🌐
Coderanch
coderanch.com › t › 553464 › java › perform-split-operations-special-characters
how to perform split operations for special characters ~!^* etc..? (Beginning Java forum at Coderanch)
September 22, 2011 - Thats because split() method takes regex not simple string. If you wan't to use some of the regex's special symbols, you must escape them with double backslash.
🌐
javaspring
javaspring.net › blog › java-split-by-special-character
Java String Splitting by Special Characters: A Comprehensive Guide — javaspring.net
A delimiter is a special character or a sequence of characters that is used to separate different parts of a string. For example, in the string "apple,banana,orange", the comma , is the delimiter. The split() method is a built-in method in the ...
🌐
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.
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