Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

เมธอด contains() ของ String ใน Java: คู่มือทีละขั้นตอน

เมธอด contains() ของ Java เช็คว่าสตริงมี substring ที่ระบุหรือไม่ คืนค่า True หรือ False รับพารามิเตอร์ 1 ตัว: substring ที่ต้องการค้นหา


เวลาทำงานกับ String มักต้องเช็คว่ามีลำดับตัวอักษรเฉพาะอยู่หรือไม่ เช่น แอปเช็คเบอร์โทรศัพท์ว่ามี country code ของสหรัฐอเมริกา (+1) หรือไม่

Java มี built-in method contains() สำหรับเช็คว่า String มีค่าที่ต้องการหรือไม่ บทช่วยสอนนี้อธิบายการใช้งาน contains() พร้อมตัวอย่าง

String ใน Java

String ใน Java เก็บข้อมูลข้อความ เป็นลำดับตัวอักษร 0 ตัวขึ้นไป ประกอบตัวอักษร สัญลักษณ์ whitespace ตัวเลข

ประกาศ String ล้อม double quotes:

String phone_number = "+1 000 000 0000";

ตัวแปร phone_number เก็บค่า +1 000 000 0000

Java String contains()

String data type มีเมธอดมากมายจัดการเนื้อหา หนึ่งในนั้นคือ contains() หา substring ใน string

contains() = built-in method เช็คว่า string มี character หรือ sequence ที่ระบุหรือไม่ ไวยากรณ์:

string_name.contains(substring);

รับพารามิเตอร์ 1 ตัว: sequence of characters ที่ต้องการค้นหา

คืนค่า 2 แบบ: มี substring -> true, ไม่มี -> false

ตัวอย่าง: เช็คการจองโต๊ะอาหาร

โปรแกรมร้านอาหาร เช็คการจองจากชื่อลูกค้า รับชื่อและ booking number เช็คนามสกุลตรงกับในระบบหรือไม่

class CheckReservation { 
    public static void main(String[] args) { 
        String name_on_file = "Jeff Clinton"; 
        String name_given_to_clerk = "Clinton"; 
        if (name_on_file.contains(name_given_to_clerk)) { 
            System.out.println("This reservation is confirmed."); 
        } else { 
            System.out.println("This reservation does not exist."); 
        } 
    } 
}

ผลลัพธ์: This reservation is confirmed.

ขั้นตอน: 1) name_on_file = ชื่อเต็มตอนจอง 2) name_given_to_clerk = นามสกุลที่ลูกค้าบอก 3) contains() เช็ค name_on_file มี name_given_to_clerk หรือไม่ มี -> confirmed, ไม่มี -> not exist

Case-Insensitive contains()

contains() เป็น case sensitive เนื้อหา substring อยู่ใน string แต่ case ต่างกัน -> false

วิธีแก้: ใช้ toLowerCase() หรือ toUpperCase() แปลงทั้ง string ต้นทางและ substring ให้ case เหมือนกัน ก่อนใช้ contains()

ตัวอย่าง: clerk พิมพ์นามสกุลเป็น lowercase -> โปรแกรมจะคืน "This reservation does not exist." แก้ด้วย toLowerCase():

class CheckReservation { 
    public static void main(String[] args) { 
        String name_on_file = "Jeff Clinton".toLowerCase(); 
        String name_given_to_clerk = "clinton".toLowerCase(); 
        if (name_on_file.contains(name_given_to_clerk)) { 
            System.out.println("This reservation is confirmed."); 
        } else { 
            System.out.println("This reservation does not exist."); 
        } 
    } 
}

ผลลัพธ์: This reservation is confirmed.

เปลี่ยน name_given_to_clerk เป็น "clinton" (lowercase) ใช้ toLowerCase() แปลงทั้งคู่ -> case ตรงกัน contains() -> true

toUpperCase() ใช้ได้เหมือนกัน เพราะทำให้ case เหมือนกัน

สรุป

เมธอด contains() ใน Java ใช้เช็คว่า String มี substring ที่ระบุหรือไม่ บทช่วยสอนนี้อธิบายการใช้งาน contains() พร้อมตัวอย่าง และการใช้ toUpperCase()/toLowerCase() ทำ case-insensitive check ตอนนี้คุณใช้ Java String contains() ได้อย่างมืออาชีพแล้ว!