You need

test.split("\\|");

split uses regular expression and in regex | is a metacharacter representing the OR operator. You need to escape that character using \ (written in String as "\\" since \ is also a metacharacter in String literals and require another \ to escape it).

You can also use

test.split(Pattern.quote("|"));

and let Pattern.quote create the escaped version of the regex representing |.

Answer from Jigar Joshi 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 - The below example tries to split a string by an escaped pipe symbol |. ... package com.mkyong.string.split; import java.util.regex.Pattern; public class StringSplitSpecialPipe { public static void main(String[] args) { String csv = "a|b|c|d"; // Three ways to escape regex special character // String[] output = csv.split("\\|"); // String[] output = csv.split("[|]"); String[] output = csv.split(Pattern.quote("|")); for (String s : output) { System.out.println(s); } } }
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
July 15, 2026 - In this case, the string is split using commas, semicolons, and pipes as delimiters. The split() method handles cases with leading delimiters by creating empty strings (“”) in the resulting array.
🌐
Stack Overflow
stackoverflow.com › questions › 43099975 › split-a-string-in-java-by-symbols
Split a string in Java by symbols - Stack Overflow
Split the string into symbols, keep the symbols as keys in the hash map and their count in corresponding hash map values. ... Map<String, Integer> tokensWithCounts = new HashMap<String, Integer>(); String[] tokens = str.split("\\s*[a-zA-Z]+\\s*"); ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - Explanation: The regex splits the string at any listed symbol.
🌐
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.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java and keep the delimiters
Split a String in Java and Keep the Delimiters | Baeldung
June 24, 2025 - First of all, let’s use the lookahead assertion “((?=@))” and split the string text around its matches: ... The lookahead regex splits the string by a forward match of the “@” symbol.
🌐
Real's HowTo
rgagnon.com › javadetails › java-0438.html
Split a string using String.split() - Real's Java How-to
Got it The String class has a split() (since 1.4) method that will return a String array. public class StringSplit { public static void main(String args[]) throws Exception{ String testString = "Real-How-To"; System.out.println (java.util.Arrays.toString(testString.split("-"))); // output : [Real, How, To] } }
🌐
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); } } } ... In the example above, we use : as the delimiter, but note that the parameter the split() method takes is a regular expression. This means it is easy to do things like split on one or more : characters by using :+.
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String myStr = "Split a string by spaces, and also punctuation."; String regex = "[,\\.\\s]"; String[] myArray = myStr.split(regex); for (String s : myArray) { System.out.println(s); }
🌐
iO Flood
ioflood.com › blog › java-string-split
How to Split Strings in Java: Methods and Examples
February 20, 2024 - In this revised example, we correctly split the string into three parts by using the escaped dot as the delimiter. As you become more comfortable with Java’s split() method, you’ll start to see the power of using regular expressions for string splitting.
🌐
ZetCode
zetcode.com › java › splitstring
Java split string - splitting strings in Java
To split a string by a dot, we need to escape it or use Pattern.quote. ... import java.util.Arrays; import java.util.regex.Pattern; void main() { String address = "127.0.0.1"; // String[] output = address.split("\\."); String[] output = address.split(Pattern.quote(".")); Arrays.stream(outp...
🌐
YouTube
youtube.com › retarget common
Splitting string with pipe character | Split String Using Regex Reserved Characters in Java | - YouTube
You will get unexpected results if you use regex reserved characters such as Pipe (|), Dollar ($), Asteric (*) to split string using the split() method of St...
Published: July 7, 2023
Views: 417
🌐
Tpoint Tech
tpointtech.com › java-string-split
Java String.split() Method - Tpoint Tech
March 19, 2025 - The split() method is defined in the String class. It splits this string around matches of the given regular expression.
🌐
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...
🌐
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 ...
🌐
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("\\*")