chars()
from the CharSequence
interface returns an IntStream
whose elements are int values representing the characters.
We can use chars()
method on a String,
a StringBuilder
, and a StringBuffer
.
The following code creates a stream of characters from a string, filters out all digits and whitespaces, and prints the remaining characters:
/*from w w w . jav a 2s. c o m*/ public class Main { public static void main(String[] args) { String str = "5 123,123,qwe,1,123, 25"; str.chars() .filter(n -> !Character.isDigit((char)n) && !Character.isWhitespace((char)n)) .forEach(n -> System.out.print((char)n)); } }
The code above generates the following result.
splitAsStream(CharSequence input)
method from the
java.util.regex.Pattern
class returns a stream of
String whose elements match the pattern.
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; //ww w. ja v a2 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.