String在Java中是一个不变的类,在Java 9中,String类添加了两个新方法。这些方法是和。这两个方法都返回IntStream对象。 chars()
codePoints()
chars()
:String类的chars()方法可以返回从此序列将char值零扩展的int流。
public IntStream chars()
import java.util.stream.IntStream; public class StringCharsMethodTest { public static void main(String args[]) { String str = "Welcome to nhooo"; IntStream intStream = str.chars(); intStream.forEach(x -> System.out.printf("-%s", (char)x)); } }
输出结果
-W-e-l-c-o-m-e- -t-o- -T-u-t-o-r-i-a-l-s-P-o-i-n-t
codePoints()
:的码点()方法可以从该序列返回码点值的流。
public IntStream codePoints()
import java.util.stream.IntStream; public class StringCodePointsMethodTest { public static void main(String args[]) { String str = "Welcome to Tutorix"; IntStream intStream = str.codePoints(); intStream.forEach(x -> System.out.print(new StringBuilder().appendCodePoint(x))); } }
输出结果
Welcome to Tutorix