Pattern split(CharSequence) method in Java with Examples

split(CharSequence) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.This method can split charSequence into an array of String’s, using the regular expression used to compile the pattern as a delimiter.so we can say that the method returns the array of strings computed by splitting the input around matches of this pattern.

Syntax:

public String[] split(CharSequence input)

Parameters: This method accepts a single parameter input which represents character sequence to be split.



Return value: This method returns the array of strings computed by splitting the input around matches of this pattern.

Below programs illustrate the split(CharSequence) method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.split(CharSequence) method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ee";
  
        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // split the text
        String[] array = pattern.split(actualString);
  
        // print array
        for (int i = 0; i < array.length; i++) {
            System.out.println("array[" + i
                               + "]=" + array[i]);
        }
    }
}

chevron_right


Output:

array[0]=g
array[1]=ksforg
array[2]=ks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.split(CharSequence) method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ke";
  
        // create the string
        // in which you want to search
        String actualString
            = "Bharat ke Veer Portal";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // split the text
        String[] array = pattern.split(actualString);
  
        // print array
        for (int i = 0; i < array.length; i++) {
            System.out.println("array[" + i
                               + "]=" + array[i]);
        }
    }
}

chevron_right


Output:

array[0]=Bharat 
array[1]= Veer Portal

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#split(java.lang.CharSequence)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.