
在Java中,String是一个不可变的类,并且在Java 9中为String类添加了两个新方法。这两个方法是chars()和codePoints()。这两个方法都返回IntStream对象。
String类的chars()方法可以返回一个从该序列中零扩展字符值的int流。
<strong>public IntStream chars()</strong>
import java.util.stream.IntStream;
public class StringCharsMethodTest {
public static void main(String args[]) {
String str = "Welcome to TutorialsPoint";
IntStream intStream = str.<strong>chars()</strong>;
intStream.forEach(x -> System.out.printf("-%s", (char)x));
}
}<strong>-W-e-l-c-o-m-e- -t-o- -T-u-t-o-r-i-a-l-s-P-o-i-n-t </strong>
codePoints()方法可以从此序列返回一个代码点值流。
<strong>public IntStream codePoints()</strong>
import java.util.stream.IntStream;
public class StringCodePointsMethodTest {
public static void main(String args[]) {
String str = "Welcome to Tutorix";
IntStream intStream = str.<strong>codePoints()</strong>;
intStream.forEach(x -> System.out.print(new StringBuilder().<strong>appendCodePoint</strong>(x)));
}
}<strong>Welcome to Tutorix</strong>
以上就是Java 9中String类添加了哪些新方法?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号