Java 9中添加到String类的新方法是什么?
String在Java中是一个不变的类,在Java9中,String类添加了两个新方法。这些方法是和。这两个方法都返回IntStream对象。chars()codePoints()
1)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
2)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
热门推荐
10 夜雨祝福语简短10字
11 商务感恩祝福语大全简短
12 媒人婚礼祝福语简短大气
13 送给老师退休祝福语简短
14 学校校长新年祝福语简短
15 女儿出阁的祝福语简短
16 谢谢同事英文祝福语简短
17 夏天客户祝福语大全 简短
18 老人去世简短祝福语