Streams from Regex

Description

splitAsStream(CharSequence input) method from the java.util.regex.Pattern class returns a stream of String whose elements match the pattern.

Example

The following code obtains a stream of strings by splitting a string using a regular expression (",").

The matched strings are printed on the standard output.


import java.util.regex.Pattern;
/*  w w w.ja va  2 s.c  o  m*/
public class Main {
  public static void main(String[] args) {
    String str = "XML,CSS,HTML"; 
    Pattern.compile(",")
    .splitAsStream(str)
    .forEach(System.out::println);

  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Tutorial »




Java Streams Tutorial