A สตริง คลาสสามารถใช้เพื่อแสดงสตริงอักขระ ตัวอักษรสตริงทั้งหมดในโปรแกรม Java ถูกนำไปใช้เป็นอินสแตนซ์ของคลาสสตริง . สตริงเป็น ค่าคงที่ และค่าของมันไม่สามารถเปลี่ยนแปลงได้ (เปลี่ยนไม่ได้ ) เมื่อสร้างแล้ว
เราสามารถพิมพ์อักขระตัวแรกของแต่ละคำเป็นสตริงโดยใช้โปรแกรมด้านล่าง
ตัวอย่าง
public class FirstCharacterPrintTest {
public static void main(String[] args) {
String str = "Welcome To Tutorials Point";
char c[] = str.toCharArray();
System.out.println("The first character of each word: ");
for (int i=0; i < c.length; i++) {
// Logic to implement first character of each word in a string
if(c[i] != ' ' && (i == 0 || c[i-1] == ' ')) {
System.out.println(c[i]);
}
}
}
} ผลลัพธ์
The first character of each word: W T T P