สตริงคือ เปลี่ยนไม่ได้ class ใน Java และมีการเพิ่มวิธีการใหม่สองวิธีในคลาส String ใน Java 9 . วิธีการเหล่านั้นคือ chars() และ codePoints() . ทั้งสองวิธีนี้ส่งคืน IntStream วัตถุ
1) chars():
The chars() วิธีการของคลาส String สามารถส่งคืนสตรีมของ int ที่ขยายค่า char เป็นศูนย์จากลำดับนี้
ไวยากรณ์
public IntStream chars()
ตัวอย่าง
import java.util.stream.IntStream;
public class StringCharsMethodTest {
public static void main(String args[]) {
String str = "Welcome to TutorialsPoint";
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():
The 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