A สตริง คลาสสามารถใช้เพื่อแสดงสตริงอักขระ ตัวอักขระสตริงทั้งหมดในโปรแกรม Java ถูกนำไปใช้เป็นอินสแตนซ์ของคลาสสตริง สตริงเป็นค่าคงที่และค่า ไม่สามารถเปลี่ยนแปลงได้ (ไม่เปลี่ยนรูป ) เมื่อสร้างแล้ว
เราสามารถใช้ startsWith() วิธีการของ สตริง class เพื่อตรวจสอบว่า string ขึ้นต้นด้วย string ที่ระบุหรือไม่ มันจะคืนค่า boolean true หรือ false
ไวยากรณ์
public boolean startsWith(String prefix)
ตัวอย่าง
public class StringStartsWithSubStringTest {
public static void main(String[] args) {
String str = "WelcomeToTutorialsPoint";
if(str.startsWith("Welcome")) {
System.out.println("Begins with the specified word!");
}
else {
System.out.println("Does not begin with the specified word!");
}
}
} ผลลัพธ์
Begins with the specified word!