Table of Contents
In this post, we will see how to split String by pipe in java.
How to split String by pipe in java
There are multiple ways to split String by pipe (|
) in Java.
Using split() method
You can use String’s split() method to split String by pipe in java.
Let’s say you want to read csv file delimited by pipe and store the data in String array. In this example, we will just simulate on header row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.util.Arrays; public class SplitByPipeJava { public static void main(String[] args) { String headerRow="Country|Capital|Population"; String[] rowArray = headerRow.split("|"); System.out.println(Arrays.toString(rowArray)); } } |
Output:
Output does not look like what we were expecting. we were expecting a String array with three elements [Country, Capital , Population].
This happened because String's split()
method takes regular expression as input and pipe is metacharacter in regex. In order to split String by pipe character, we need to escape pipe in split() method.
There are two ways to escape pipe character in split method.
Using \ to escape pipe
You can use \\
to escape pipe character. Let’s see with example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.util.Arrays; public class SplitByPipeJava { public static void main(String[] args) { String headerRow="Country|Capital|Population"; String[] rowArray = headerRow.split("\\|"); System.out.println(Arrays.toString(rowArray)); } } |
Output:
As you can see, we got expected out now.
Using Pattern.quote() to escape pipe
You can also use Pattern’s quote()
method to escape pipe character. Let’s see with example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.java2blog.datastructures; import java.util.Arrays; import java.util.regex.Pattern; public class SplitByPipeJava { public static void main(String[] args) { String headerRow="Country|Capital|Population"; String[] rowArray = headerRow.split(Pattern.quote("|")); System.out.println(Arrays.toString(rowArray)); } } |
Output:
Further reading:
Using StringTokenizer
You can use StringTokenizer
class to split String by pipe in java.
Here are steps to do it.
- Create
StringTokenizer
object with constructor which takes String and delimiter as input. - Since we need to split String by
|
, we will pass delimiter as pipe. - Use while loop with
hasMoreToken()
method to check if there are more tokens available in String. - Use
nextToken()
method to get token based on delimiter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog.datastructures; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.regex.Pattern; public class SplitByPipeUsingStringTokenizerJava { public static void main(String[] args) { String headerRow="Country|Capital|Population"; String[] result=splitUsingStringTokenizerByPipe(headerRow,"|"); System.out.println(Arrays.toString(result)); } public static String[] splitUsingStringTokenizerByPipe(String str, String delimiter) { StringTokenizer stringTokenizer = new StringTokenizer(str, delimiter); ArrayList<String> tokensStr = new ArrayList<String>(str.length()); while(stringTokenizer.hasMoreTokens()) { tokensStr.add(stringTokenizer.nextToken()); } return tokensStr.toArray(new String[0]); } } |
Output:
Using Pattern class
You can also use combination of compile()
and split()
method of Pattern class to split String by pipe in java.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.java2blog.datastructures; import java.util.Arrays; import java.util.regex.Pattern; public class SplitByPipeUsingPatternJava { public static void main(String[] args) { String headerRow="Country|Capital|Population"; String[] rowArray = Pattern.compile("\\|").split(headerRow); System.out.println(Arrays.toString(rowArray)); } } |
Output:
Although this looks more complicated than String’s split()
method.
That’s all about how to split String by pipe in java.