Split String by pipe(|) in java

Split String by pipe in java

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.

Output:

[C, o, u, n, t, r, y, |, C, a, p, i, t, a, l, |, P, o, p, u, l, a, t, i, o, n]

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:

Output:

[Country, Capital, Population]

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.

Output:

[Country, Capital, Population]

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.

Output:

[Country, Capital, Population]

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:

Output:

[Country, Capital, Population]

Although this looks more complicated than String’s split() method.

That’s all about how to split String by pipe in java.

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.