ภาพรวม
บทความนี้สาธิตการใช้เมธอด String.endsWith() เพื่อตรวจสอบว่าข้อความใน EditText ลงท้ายด้วยคำเฉพาะหรือไม่
ขั้นตอนที่ 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;
}
// ตรวจสอบว่าลงท้ายด้วย "point" หรือไม่
boolean endsWithPoint = input.endsWith("point");
resultTextView.setText("ลงท้ายด้วย 'point': " + endsWithPoint);
}
});
}
}
การใช้งาน endsWith()
| โค้ด | ผลลัพธ์ | คำอธิบาย |
|---|---|---|
| "tutorialspoint".endsWith("point") | true | ลงท้ายด้วย "point" |
| "tutorialspoint".endsWith("Point") | false | Case-sensitive |
| "hello".endsWith("") | true | Empty string always true |
| "hello".endsWith(null) | NPE | Throw NullPointerException |
Case-Insensitive Version
boolean endsWithIgnoreCase = input.toLowerCase().endsWith("point".toLowerCase());
// หรือ
boolean endsWithIgnoreCase = input.regionMatches(true, input.length() - "point".length(), "point", 0, "point".length());
กรณีใช้งานจริง
- ตรวจสอบนามสกุลไฟล์:
filename.endsWith(".pdf") - ตรวจสอบ URL:
url.endsWith("/") - Validate input format
- Routing/Navigation logic
ทดสอบ
ป้อน "tutorialspoint" → แสดง "true"
ป้อน "tutorials" → แสดง "false"
ป้อน "POINT" → แสดง "false" (case-sensitive)