The pipe symbol is special in a regexp (it marks alternatives), you need to escape it. Depending on the java version you are using this could well explain your unpredictable results.

class t {
    public static void main(String[]_)
    {
        String temp = "0|0";
        String[] splitString = temp.split("\\|");

        for (int i=0; i<splitString.length; i++)
            System.out.println("splitString["+i+"] is " + splitString[i]);
    }       
}

outputs

splitString[0] is 0
splitString[1] is 0

Note that one backslash is the regexp escape character, but because a backslash is also the escape character in java source you need two of them to push the backslash into the regexp.

Answer from crazyscot on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ split-a-string-in-java-with-delimiter
How to Split a String in Java with Delimiter? - GeeksforGeeks
July 23, 2025 - In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2017 โ€บ 01 โ€บ how-to-split-string-based-on-delimiter-in-java.html
How to Split String based on delimiter in Java? Example Tutorial
This is similar to the earlier example but it's more common because CSV is a popular format to export data from databases, tables, or XLS files. You can split a comma-delimited String by passing "," to split() method as shown in the following ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ how-to-split-a-string-in-java-with-delimiter
How to Split a String in Java with Delimiter
September 21, 2022 - Here, we passing dot, comma and pipe char //and a space as delimiters. String[] strArray = str.split("[,.| ]"); for(String s: strArray){ System.out.println(s); } } } ... Example of split(String regex, int limit): This is second variant of split() method. public class JavaExample { public static ...
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ java tutorials โ€บ java string split() method with example
Java String split() Method with Example
July 30, 2026 - Split a String in Java using the split() method, which breaks text wherever a delimiter matches and returns the pieces as a String array.
๐ŸŒ
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.
๐ŸŒ
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 - A character class such as [,;] splits on any one of the listed characters, and adding + treats a run of delimiters as one. An alternation with | handles delimiters that are longer than one character, and \\s* around a delimiter removes the spaces ...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to split a string in java
How to split a string in Java | mkyong.com
February 9, 2022 - If we want to split a string using one of the special regex characters as the delimiter, we must escape it. For example, if we want to split a string by a vertical bar or pipe symbol | (special regex character), which means or in the regex, ...
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ strings โ€บ .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - The .split() method takes a string and splits it into an array of substrings based on a pattern delimiter. The pattern provided is a regular expression that will split the original string based on where the pattern is matched in the string.
๐ŸŒ
YouTube
youtube.com โ€บ oggi ai - artificial intelligence today
Java: Split String at Delimiter | break String into Array of Substrings - YouTube
This video explains how to split a string in Java into an array of substrings, at one or more delimiter characters such as space, period, or comma. Code: htt...
Published: April 20, 2023
Views: 1K
Top answer
1 of 10
66

There is no need to set the delimiter by breaking it up in pieces like you have done.

Here is a complete program you can compile and run:

import java.util.Arrays;
public class SplitExample {
    public static final String PLAYER = "1||1||Abdul-Jabbar||Karim||1996||1974";
    public static void main(String[] args) {
        String[] data = PLAYER.split("\\|\\|");
        System.out.println(Arrays.toString(data));
    }
}

If you want to use split with a pattern, you can use Pattern.compile or Pattern.quote.

To see compile and quote in action, here is an example using all three approaches:

import java.util.Arrays;
import java.util.regex.Pattern;
public class SplitExample {
    public static final String PLAYER = "1||1||Abdul-Jabbar||Karim||1996||1974";
    public static void main(String[] args) {
        String[] data = PLAYER.split("\\|\\|");
        System.out.println(Arrays.toString(data));

        Pattern pattern = Pattern.compile("\\|\\|");
        data = pattern.split(PLAYER);
        System.out.println(Arrays.toString(data));

        pattern = Pattern.compile(Pattern.quote("||"));
        data = pattern.split(PLAYER);
        System.out.println(Arrays.toString(data));
    }
}

The use of patterns is recommended if you are going to split often using the same pattern. BTW the output is:

[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
2 of 10
36

Use the Pattern#quote() method for escaping ||. Try:

final String[] tokens = myString.split(Pattern.quote("||"));

This is required because | is an alternation character and hence gains a special meaning when passed to split call (basically the argument to split is a regular expression in string form).

๐ŸŒ
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 - Split a string in Java using the String.split() method, which accepts a regex delimiter, requires escaping special characters, and handles edge case inputs
๐ŸŒ
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.
๐ŸŒ
Codefinity
codefinity.com โ€บ courses โ€บ v2 โ€บ 0bfa1111-8677-478f-bcd3-315dff1d7d40 โ€บ 421ab035-77e8-4fef-abb9-488c91c2d1f9 โ€บ 433c4bac-9f33-4b48-9be0-cdc81423c941
Learn Method split() | String Advanced
The split(String delimiter) method splits a string into an array of substrings based on the specified delimiter (a character or sequence of characters). It returns an array where each element is a substring from the original string, separated ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - Explanation: In this example, a string "Geeks for Geeks" is created and split using the split(" ") method, where a space is used as the delimiter. This divides the string into individual words and stores them in an array arr.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ string โ€บ split a string in java: split(), pattern, scanner and more
Split a String in Java: split(), Pattern, Scanner and More
3 weeks ago - To split a string in Java, we call String.split() with a delimiter regex, which returns a String[] of the parts between the matches.
Top answer
1 of 4
22

I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.

For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.

For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)

2 of 4
6

As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.

However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...

Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.

Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM

HashMap<String, String[]> cache = new HashMap();

int compare(String s1, String s2) {
   String[] cached1 = cache.get(s1);
   if (cached1  == null) {
      cached1 = mySuperSplitter(s1):
      cache.put(s1, cached1);
   }
   String[] cached2 = cache.get(s2);
   if (cached2  == null) {
      cached2 = mySuperSplitter(s2):
      cache.put(s2, cached2);
   }

   return compareAsArrays(cached1, cached2);  // real comparison done here
}
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-multiple-delimiters-in-java
Split String by Multiple Delimiters in Java
September 30, 2022 - public class JavaExample { public static void main (String[] args) { //A string with multiple delimiters String str = "Text1,Text2#Text3 Text4:Text5.Text6"; //Specified the delimiters inside brackets, there is a //whitespace after # to consider space as delimiter as well String[] strArray = str.split("[,# :.]"); for(String s: strArray){ System.out.println(s); } } }