English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Pattern class in the java.util.regex package is the compiled representation of regular expressions.
This class's splitAsStream() method accepts a CharSequence object, which represents the input string as a parameter, and in each match, it splits the given string into a new substring, and returns the result as a stream containing all substrings.
import java.util.regex.Pattern; import java.util.stream.Stream; public class SplitAsStreamMethodExample { public static void main( String args[] ) { //Regular expression to find numbers String regex = "(\\s)(\\d)(\\s)"; String input = " 1 Name: Radha, age:25 2 Name: Ramu, age:32" + " 3 Name: Rajeev, age:45 4 Name: Raghu, age:35" + " 5 Name: Rahman, age:30"; //Compile regular expression Pattern pattern = Pattern.compile(regex); //Verify if a match occurs if(pattern.matcher(input).find()) System.out.println("Given String contains digits"); else System.out.println("Given String does not contain digits"); //Split String Stream<String> stream = pattern.splitAsStream(input); Object obj[] = stream.toArray(); for(int i = 0; i < obj.length; i++) { System.out.println(obj[i]); } } }
Output Result
Given String contains digits Name: Radha, age:25 Name: Ramu, age:32 Name: Rajeev, age:45 Name: Raghu, age:35 Name: Rahman, age:30