Convert Pipe Delimited String to List Java

Convert Pipe Delimited String to List Java | The characters that split the string into tokens are termed delimiters. These delimiters can be a comma, pipe, space, colon, semicolon(s), and more.

In this section, we will write a java program to convert the delimited string to a list. The delimiter we have used as a pipe (|). The pipe delimited strings are the strings separated by a pipe character.

Usually, we separate words using space, or commas, but in some situations, they are separated through the pipe character(|). See the below examples of pipe delimited strings:-
1) “Country | Capital | Population”
2) ”a | b | c”
3) ”1| 2 | 3 | 4 | 5”

The above example is just to show the pipe as a delimiter but our problem is to convert the above pipe-delimited string to a list. To do this we need to do two steps:-
1) Remove the pipe delimiter
2) Convert the obtained array to list

Program to Convert Pipe Delimited String to List Java

Let us develop a program to convert pipe delimited string to list Java. We will initialize a string containing pipe delimiters.

import java.util.Arrays;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      String string = "Java|Python|JavaScript|C++|PHP";

      List<String> list = Arrays.asList(string.split("\\|"));
      System.out.println("List :" + list);
      System.out.println("Size of the list: " + list.size());
   }
}

Output:-

List :[Java, Python, JavaScript, C++, PHP]
Size of the list: 5

In the above method, we have called the split() method to split based on the pipe delimiter. If we want to split based on pipe character then we have to use the escape sequence “\\|”. If we try to use “|” in the split method then it will split each and every character. Below example demonstrates it:-

String string = "Java|Python|JavaScript|C++|PHP";
String[] strArray = string.split("|");
System.out.println("String array: " + Arrays.toString(strArray));
System.out.println("String array size: " + strArray.length);

Output:-

String array: [J, a, v, a, |, P, y, t, h, o, n, |, J, a, v, a, S, c, r, i, p, t, |, C, +, +, |, P, H, P]
String array size: 30

String string = "Java|Python|JavaScript|C++|PHP";
String[] strArray = string.split("\\|");
System.out.println("String array: " + Arrays.toString(strArray));
System.out.println("String array size: " + strArray.length);

Output:-

String array: [Java, Python, JavaScript, C++, PHP]
String array size: 5

In the program to convert pipe delimited string to list Java, after splitting based on pipe character we have used the Arrays.asList() method to convert array to list. But you can also do it in another way like below.

import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      String string = "Java|Python|JavaScript|C++|PHP";
      String[] strArray = string.split("\\|");

      List<String> list = new ArrayList<>();
      for (String str : strArray) {
         list.add(str);
      }
      System.out.println("List: " + list);
   }
}

Output:-

List: [Java, Python, JavaScript, C++, PHP]

If the string contains numbers then while adding it to the list we can convert them into the int type value and then add it to the list.

import java.util.ArrayList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      String string = "10|20|99|55|9";
      String[] strArray = string.split("\\|");
      List<Integer> list = new ArrayList<>();

      for (String str : strArray) {
         list.add(Integer.valueOf(str));
      }
      System.out.println("List: " + list);
   }
}

Output:-

List: [10, 20, 99, 55, 9]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.