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