Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String delimiter in string.split method

I have following data:

1||1||Abdul-Jabbar||Karim||1996||1974 

I want to delimit the tokens.

Here the delimiter is "||".

My delimiter setter is:

public void setDelimiter(String delimiter) {     char[] c = delimiter.toCharArray();     this.delimiter = "\"" + "\\" + c[0] + "\\" + c[1] + "\"";     System.out.println("Delimiter string is: " + this.delimiter); } 

However,

String[] tokens = line.split(delimiter); 

is not giving the required result.

like image 679
Vicky Avatar asked Aug 11 '11 05:08

Vicky


People also ask

How do you split a string with a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

Which string function splits a string based on a delimiter?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

How do you delimiter a string?

Let's use the split() method and split the string by a comma. In the above example, the string object is delimited by a comma. The split() method splits the string when it finds the comma as a delimiter. Let's see another example in which we will use multiple delimiters to split the string.


2 Answers

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] 
like image 195
Ray Toal Avatar answered Sep 26 '22 01:09

Ray Toal


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).

like image 28
Sanjay T. Sharma Avatar answered Sep 24 '22 01:09

Sanjay T. Sharma