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 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短