March 4, 2022

Java String split() Method

Java String split() method splits the string around matches of the given regular expression. The split() method returns string array which contains all the words computed by splitting this string around matches of the given regular expression.

In Java String class there are two variants of split() method-

String[] split(String regex, int limit) 

String[] split(String regex)

Method parameters are-

regex- The delimiting regular expression
limit- The limit parameter controls the number of times the pattern is applied, it may have following three values.
  • If limit > 0 then the pattern will be applied at most limit - 1 times. Returned String array's length will be no greater than limit, and the array's last entry will contain all input beyond the last matched delimiter.
  • If limit = zero then the pattern will be applied as many times as possible, returned String array can have any length and trailing empty strings will be discarded.
  • If limit < 0 then the pattern will be applied as many times as possible and the returned String array can have any length.

Java String split() method examples

1. Splitting data delimited using spaces, for that “\\s+ ” regex is used which matches any number of spaces.

import java.util.Arrays;

public class SplitString {
  public static void main(String[] args) {
    String str = "A001  BOA Nicki 12000";
    // Matches any number of spaces
    String[] data = str.split("\\s+");
    System.out.println(Arrays.toString(data));
  }
}
Output
[A001, BOA, Nicki, 12000]

2. Splitting data delimited using tab.

public class SplitString {
  public static void main(String[] args) {
    String str = "A001	BOA	Nicki	12000";
    String[] data = str.split("\t");
    System.out.println(Arrays.toString(data));
  }
}
Output
[A001, BOA, Nicki, 12000]

3. Splitting data delimited using pipe (|). Note that pipe symbol has to be escaped using escape character (\).

public class SplitString {
  public static void main(String[] args) {
    String str = "A001|BOA|Nicki|12000";
    String[] data = str.split("\\|");
    // Accessing name field
    System.out.println("Name- "+ data[2]);
  }
}
Output
Name- Nicki

4. Splitting comma separated values (CSV) using split() method.

public class SplitString {
  public static void main(String[] args) {
    String str = "A001,BOA,Nicki,12000";
    String[] data = str.split(",");
    for(String s : data)
      System.out.println(s);
  }
}
Output
A001
BOA
Nicki
12000

5. Using split() method with limit parameter.

public class SplitString {
  public static void main(String[] args) {
    String str = "A001,BOA,Nicki,12000";
    String[] data = str.split(",", 2);
    for(String s : data)
      System.out.println(s);
  }
}
Output
A001
BOA,Nicki,12000

Since value of limit is 2, pattern is applied 1 (limit - 1) time only. Array's last entry contains all input beyond the last matched delimiter.

That's all for the topic Java String split() Method. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment