ภาพรวม
บทความนี้สาธิตการใช้ String.contains() ตรวจสอบว่าข้อความใน EditText มีคำ/ข้อความย่อย (Substring) ที่ต้องการหรือไม่
ขั้นตอนที่ 1: สร้างโปรเจกต์
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ป้อนข้อความ" />
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="ตรวจสอบ" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="25sp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (java/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private Button checkButton;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.name);
checkButton = findViewById(R.id.click);
resultTextView = findViewById(R.id.textview);
checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = nameEditText.getText().toString().trim();
if (input.isEmpty()) {
nameEditText.setError("กรุณาป้อนข้อความ");
return;
}
String searchTerm = "tutorialspoint";
boolean contains = input.contains(searchTerm);
resultTextView.setText("มีคำ '" + searchTerm + "': " + contains);
}
});
}
}
คำอธิบาย contains()
- คืนค่า
boolean: true ถ้าพบ substring, false ถ้าไม่พบ - Case-sensitive (แยกตัวพิมพ์เล็ก/ใหญ่)
- Throw
NullPointerExceptionถ้า argument เป็น null - ใช้ CharSequence interface รองรับ String, StringBuilder, Spanned ฯลฯ
ตัวอย่าง
"tutorialspoint is best".contains("tutorialspoint") // true
"tutorialspoint is best".contains("Tutorialspoint") // false (case-sensitive)
"hello".contains("") // true (empty string always found)
"hello".contains(null) // NullPointerException
Case-Insensitive Contains
// วิธีที่ 1: toLowerCase (Simple) boolean found = input.toLowerCase().contains(searchTerm.toLowerCase()); // วิธีที่ 2: Pattern (Regex) - ยืดหยุ่นกว่า import java.util.regex.Pattern; boolean found = Pattern.compile(Pattern.quote(searchTerm), Pattern.CASE_INSENSITIVE).matcher(input).find();
กรณีใช้งานจริง
- Content filtering / Search
- Validation: ตรวจสอบคำหยาม, Spam keywords
- Tag/Category detection
- URL/Email domain checking
Performance Note
สำหรับข้อความยาวหรือค้นหาบ่อย ควรใช้:
Patterncompiled ไว้ reuse- Trie/Aho-Corasick สำหรับหลายคำพร้อมกัน
- Full-text search engine (Room FTS, SQLite FTS, Elasticsearch)
ทดสอบ
ป้อน "tutorialspoint is best website" → true
ป้อน "I love android" → false
ป้อน "TUTORIALSPOINT" → false (case-sensitive)