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:
import java.util.regex.*;
import java.util.stream.*;
public class GFG {
public static void main(String[] args)
{
String REGEX = "geeks" ;
String actualString
= "Welcome to geeks for geeks" ;
Pattern pattern = Pattern.compile(REGEX);
Stream<String> stream
= pattern
.splitAsStream(actualString);
stream.forEach(System.out::println);
}
}
|
Program 2:
import java.util.regex.*;
import java.util.stream.*;
public class GFG {
public static void main(String[] args)
{
String REGEX = "ee" ;
String actualString
= "aaeebbeecceeddee" ;
Pattern pattern = Pattern.compile(REGEX);
Stream<String> stream
= pattern
.splitAsStream(actualString);
stream.forEach(System.out::println);
}
}
|
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#splitAsStream(java.lang.CharSequence)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Mar, 2019
Like Article
Save Article