Thursday 26 March 2020

Regular Expression: Split a string by the pipe symbol

Suppose you have a string with | characters in it. Since | is a regular expression character we need to escape it while splitting the string like below.

String[] tokens = str.split("\\|");

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  String str = "Hello|How|Are|You";
  
  String[] tokens = str.split("\\|");
  
  for(String token : tokens) {
   System.out.println(token);
  }

 }

}

Output
Hello
How
Are
You


Previous                                                    Next                                                    Home

No comments:

Post a Comment