在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中文网其他相关文章!