String เป็นอ็อบเจกต์แทน ลำดับตัวอักษรที่ไม่เปลี่ยนแปลง (immutable) ใช้คลาส java.lang.String สร้างอ็อบเจกต์สตริง
ใช้เมธอด endsWith() ตรวจสอบว่าสตริงลงท้ายด้วย substring ที่ระบุ คืนค่า boolean (true/false)
ไวยากรณ์
public boolean endsWith(String suffix)
ตัวอย่าง: ตรวจสอบคำท้าย
public class StringEndsWithSubStringTest {
public static void main(String[] args) {
String str = "WelcomeToTutorialsPoint";
if (str.endsWith("Point")) {
System.out.println("Ends with the specified word!");
} else {
System.out.println("Does not end with the specified word!");
}
}
}
ผลลัพธ์
Ends with the specified word!
เมธอดที่เกี่ยวข้อง: startsWith() (ตรวจคำหน้า), contains() (ตรวจสตริงย่อยทั่วไป)