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

วิธีนับจำนวนคำใน TextView บน Android

การนับจำนวนคำ (Word Count) ใน TextView

ตัวอย่างสาธิตการแยกข้อความใน TextView ด้วย Regular Expression แล้วนับจำนวนคำ แสดงผลผ่าน Toast

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

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout XML (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"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

ขั้นตอนที่ 3: โค้ด Java (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView text;

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

        // ตั้งค่าข้อความยาว (Lorem Ipsum)
        text.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
                "Lorem Ipsum has been the industry's standard dummy text ever since the " +
                "1500s, when an unknown printer took a galley of type and scrambled it to " +
                "make a type specimen book. It has survived not only five centuries, but " +
                "also the leap into electronic typesetting, remaining essentially unchanged. " +
                "It was popularised in the 1960s with the release of Letraset sheets " +
                "containing Lorem Ipsum passages, and more recently with desktop publishing " +
                "software like Aldus PageMaker including versions of Lorem Ipsum.");

        // แยกคำด้วย Regex: \\s+ = whitespace 1 ตัวขึ้นไป
        String[] words = text.getText().toString().split("\\s+");

        // แสดงจำนวนคำผ่าน Toast
        Toast.makeText(MainActivity.this, "Word count: " + words.length, Toast.LENGTH_LONG).show();
    }
}

การทดสอบ

รันแอป จะแสดง Toast แจ้งจำนวนคำในข้อความ Lorem Ipsum

วิธีนับจำนวนคำใน TextView บน Android

รายละเอียด Regular Expression

  • \\s+ = match whitespace 1 ตัวขึ้นไป (space, tab, newline)
  • จัดการได้ทั้งช่องว่างหลายช่อง, Tab, Descent line
  • ไม่ต้องกังวลเรื่อง multiple spaces

ข้อจำกัดและปรับปรุง

โค้ดปรับปรุงสำหรับความแม่นยำ

String textContent = text.getText().toString().trim();
if (!textContent.isEmpty()) {
    String[] words = textContent.split("\\s+");
    int count = 0;
    for (String w : words) if (!w.isEmpty()) count++;
    Toast.makeText(this, "Words: " + count, Toast.LENGTH_LONG).show();
}
ปัญหาวิธีแก้
นับเครื่องหมายวรรคตอนเป็นคำแยกใช้ split("[\\s\\p{Punct}]+") หรือ filter ด้วย Pattern
ภาษาไทยไม่มีช่องว่างระหว่างคำต้องใช้ Thai Word Segmentation library (เช่น ThaiTokenizer)
Empty string เริ่ม/ท้ายใช้ .trim() ก่อน split หรือ filter empty strings