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

วิธีลบสระออกจากข้อความใน TextView บน Android

การลบสระ (Vowels) ออกจากข้อความใน Android

ตัวอย่างนี้สาธิตวิธีนำข้อความจาก EditText มาประมวลผลลบสระภาษาอังกฤษ (a, e, i, o, u ทั้งพิมพ์เล็กและใหญ่) แล้วแสดงผลใน TextView โดยใช้ Regular Expression

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 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:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="24dp">

    <EditText
        android:id="@+id/etInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ป้อนข้อความภาษาอังกฤษ"
        android:inputType="text" />

    <Button
        android:id="@+id/btnProcess"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="ลบสระและแสดงผล" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:textColor="#2196F3" />
</LinearLayout>

ขั้นตอนที่ 3: 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 android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText etInput;
    private TextView tvResult;
    private Button btnProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etInput = findViewById(R.id.etInput);
        tvResult = findViewById(R.id.tvResult);
        btnProcess = findViewById(R.id.btnProcess);

        btnProcess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = etInput.getText().toString();
                if (!input.isEmpty()) {
                    // Regex: [AEIOUaeiou] = ตัวอักษรใดๆ ในชุดนี้ (สระภาษาอังกฤษ)
                    // แทนที่ด้วย empty string เพื่อลบออก
                    String result = input.replaceAll("[AEIOUaeiou]", "");
                    tvResult.setText(result);
                    Toast.makeText(MainActivity.this, "ลบสระเสร็จสิ้น", Toast.LENGTH_SHORT).show();
                } else {
                    etInput.setError("กรุณาป้อนข้อความ");
                }
            }
        });
    }
}

คำอธิบาย Regular Expression

String result = input.replaceAll("[AEIOUaeiou]", "");
  • [...] : Character class - จับคู่ตัวอักษรใดตัวหนึ่งภายในวงเล็บ
  • AEIOUaeiou : สระภาษาอังกฤษทั้งพิมพ์ใหญ่และเล็ก
  • replaceAll : แทนที่ทุกตัวที่ตรงกับ pattern

ตัวอย่างผลลัพธ์

InputOutput (ลบสระแล้ว)
Hello WorldHll Wrld
AndroidAndrd
SAIRAMSRM
ProgrammingPrgrmmng
AEIOU aeiou(ว่างเปล่า)

ขยายความสามารถ

รองรับสระภาษาไทย

// สระภาษาไทย: ะ, ำ, ำ, ิ, ี, ึ, ื, ุ, ู, เ, แ, โ, ใ, ไ
String thaiVowelsRemoved = input.replaceAll("[ะำิีึืุูเแโใไ]", "");

นับจำนวนสระที่ถูกลบ

int vowelCount = input.length() - result.length();
// หรือใช้ Pattern/Matcher
Pattern pattern = Pattern.compile("[AEIOUaeiou]");
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) count++;

เก็บเฉพาะสระ (กลับกัน)

String onlyVowels = input.replaceAll("[^AEIOUaeiou]", ""); // ^ = NOT

กรณีการใช้งานจริง

  • เกมคำ (Word games) - ลบสระเพื่อสร้างปริศนา
  • การประมวลผลภาษาธรรมชาติ (NLP) - เตรียมข้อมูล
  • การสร้างรหัสผ่าน/username จากชื่อ
  • การบีบอัดข้อความเบื้องต้น