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.
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.
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.
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.
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.
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]
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With