ภาพรวม
บทความนี้สาธิตการใช้ String.isEmpty() ตรวจสอบว่าข้อความใน EditText ว่างหรือไม่ และแสดง Index ของคำค้นหา
ขั้นตอนที่ 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();
// ตรวจสอบว่าว่างหรือไม่ (isEmpty = length == 0)
if (input.isEmpty()) {
nameEditText.setError("ข้อความว่างเปล่า");
return;
}
// หาตำแหน่งของคำว่า "sai"
String searchTerm = "sai";
int index = input.indexOf(searchTerm);
if (index >= 0) {
resultTextView.setText("พบ '" + searchTerm + "' ที่ตำแหน่ง Index: " + index);
} else {
resultTextView.setText("ไม่พบ '" + searchTerm + "' ในข้อความ");
}
}
});
}
}
ความแตกต่าง: isEmpty() vstrim().isEmpty() vs == ""
| เมธอด | คืนค่า true เมื่อ | หมายเหตุ |
|---|---|---|
| str.isEmpty() | length() == 0 | Null-safe? ไม่ - Throw NPE ถ้า str=null |
| str.trim().isEmpty() | มีแต่ whitespace | แนะนำสำหรับ User Input |
| TextUtils.isEmpty(str) | null หรือ length==0 | Android Utility - Null-safe |
| str == null || str.isEmpty() | null หรือว่าง | Manual check |
แนะนำ: ใช้ TextUtils.isEmpty()
import android.text.TextUtils;
if (TextUtils.isEmpty(input)) {
// ว่างหรือ null
}
// API 26+ (Android 8.0+)
if (input.isBlank()) { // Java 11+ / API 24+ (Android 7.0+ with desugaring)
// ว่าง หรือมีแต่ whitespace
}
indexOf() และ lastIndexOf()
String text = "Krishna sai";
text.indexOf("sai") // 8 (เจอครั้งแรก)
text.lastIndexOf("a") // 10 (เจอครั้งสุดท้าย)
text.indexOf("xyz") // -1 (ไม่เจอ)
ทดสอบ
ป้อน "Krishna sai" → "พบ 'sai' ที่ตำแหน่ง Index: 8"
ป้อน "Hello" → "ไม่พบ 'sai' ในข้อความ"
กดปุ่มโดยไม่พิมพ์ → Error "ข้อความว่างเปล่า"