Pattern splitAsStream() Method in Java with Examples

splitAsStream() method of a Pattern class used to return a stream of String from the given input sequence passed as parameter around matches of this pattern.this method is the same as the method split except that what we get back is not an array of String objects but a stream. If this pattern does not match any subsequence of the input then the resulting stream has just one element, namely the input sequence in string form.

Syntax:

public Stream<String> splitAsStream(CharSequence input)

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

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

Below programs illustrate the splitAsStream() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.splitAsStream(CharSequence input) method
  
import java.util.regex.*;
import java.util.stream.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "geeks";
  
        // create the string
        // in which you want to search
        String actualString
            = "Welcome to geeks for geeks";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // split the text
        Stream<String> stream
            = pattern
                  .splitAsStream(actualString);
  
        // print array
        stream.forEach(System.out::println);
    }
}

chevron_right


Output:

Welcome to 
 for

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.splitAsStream(CharSequence input) method
  
import java.util.regex.*;
import java.util.stream.*;
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
            = "aaeebbeecceeddee";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // split the text
        Stream<String> stream
            = pattern
                  .splitAsStream(actualString);
  
        // print array
        stream.forEach(System.out::println);
    }
}

chevron_right


Output:

aa
bb
cc
dd

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#splitAsStream(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.