Split Function in Java

In this post, you will be learning about the split function in java and how you can use it with-in your java program.

Introduction

The string split() method splits the given string around matches of the given regular expression.

Syntax

public String split(String regex)
public String split(String regex, int limit)

Parameters:

  • regex – a delimiting regular expression
  • Limit – the result threshold

Parameters:

  • regex – a delimiting regular expression
  • Limit – the result threshold
  • Limit parameter may have 3 values:
    • limit > Limit applied is -1 times, the resulting array’s length will not be more than n, and the resulting array’s last entry will contain all input beyond the last matched pattern.
    • limit < 0: Here, the pattern will be applied as many times as possible, and the resulting array can be of any size.
    • limit = 0 : Her, the resulting array can be of any size, and trailing empty strings will be discarded.

Sample program

public class Main { 
    public static void main(String args[]) 
    { 
        String str = "[email protected]"; 
        String[] arrOfStr = str.split("@", 5); 
  
        for (String a : arrOfStr) 
            System.out.println(a); 
    } 
} 

Output:

Coders
Editor

Sample program using split function() with negative parameter and Zero parameter

public class Main{
   public static void main(String args[]){
        String s = new String("codersceditor");
    String arr2[]= s.split("c", 0);
    System.out.println("Spliting with Zero Limit:");
    for (String str2: arr2){
       System.out.println(str2);
    }
    String arr1[]= s.split("c", -1);
    System.out.println("Spliting with Negative Limit:");
    for (String str: arr1){
       System.out.println(str);
    }

       System.out.println("Split function executed successfully");
   }
}

Output

Spliting with Zero Limit:

oders
editor
Spliting with Negative Limit:

oders
editor
Split function executed successfully

public String[] split(String regex)

This variant of split method takes a regular expression as parameter, and breaks the given string around matches of this regular expression regex. Here by default limit is 0.

Parameters:

regex – a delimiting regular expression

Sample program

public classMain { 
    public static void main(String args[]) 
    { 
        String str = "CodersEditor:An online tutorial website"; 
        String[] arrOfStr = str.split(":"); 
  
        for (String a : arrOfStr) 
            System.out.println(a); 
    } 
} 

Output

CodersEditor 
An online tutorial website

Example of Split function with Regular Expression

public class Main { 
    public static void main(String args[]) 
    { 
        String str = "CodersabcEditor"; 
        String[] arr = str.split("abc"); 
  
        for (String s : arr) 
            System.out.println(s); 
    } 
}

Output

Coders
Editor

Split function in Java
close

Copy and paste this code to display the image on your site

%d bloggers like this: