// Word count — split on whitespace, filter empty strings
const text = 'Hello world! How are you?'
const words = text.trim().split(/\s+/).filter(Boolean)
console.log(words.length) // → 5
// Character count
console.log(text.length) // → 27 (with spaces)
console.log(text.replace(/\s/g, '').length) // → 22 (without spaces)
// Sentence count — split on sentence-ending punctuation
const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0)
console.log(sentences.length) // → 2
// Reading time estimate (238 wpm average)
const readingMin = words.length / 238
console.log(Math.ceil(readingMin)) // → 1 min
Python
import re
text = 'Hello world! How are you?'
# Word count
words = text.split()
print(len(words)) # → 5
# Character counts
print(len(text)) # → 27 (with spaces)
print(len(text.replace(' ', ''))) # → 22 (without spaces)
# Sentence count
sentences = [s for s in re.split(r'[.!?]+', text) if s.strip()]
print(len(sentences)) # → 2
# Paragraph count
multiline = """First paragraph.
Second paragraph."""
paragraphs = [p for p in multiline.split('\n\n') if p.strip()]
print(len(paragraphs)) # → 2
Go
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
text := "Hello world! How are you?"
// Word count
words := strings.Fields(text)
fmt.Println(len(words)) // → 5
// Character count (rune-aware for Unicode)
fmt.Println(len([]rune(text))) // → 27
// Characters without spaces
noSpaces := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, text)
fmt.Println(len([]rune(noSpaces))) // → 22
}
CLI (bash)
# Word count
echo "Hello world" | wc -w
# → 2
# Character count (bytes — use wc -m for multibyte chars)
echo -n "Hello world" | wc -m
# → 11
# Line count
echo -e "line1\nline2\nline3" | wc -l
# → 3
# Count words in a file
wc -w < article.txt
# → 4230
คำถามที่พบบ่อย
เครื่องนับคำนิยาม "คำ" อย่างไร?
เครื่องมือนี้แยกข้อความตามช่องว่าง (เว้นวรรค แท็บ ขึ้นบรรทัดใหม่) และนับโทเค็นที่ไม่ว่างเปล่าที่ได้ คำที่ใช้ยัติภังค์ เช่น "well-known" นับเป็นหนึ่งคำ ซึ่งตรงกับพฤติกรรมของ Microsoft Word และ Google Docs สำหรับข้อความภาษาอังกฤษ สำหรับภาษากลุ่ม CJK การแยกด้วยช่องว่างนับจำนวนคำต่ำกว่าความเป็นจริง เนื่องจากอักขระเหล่านั้นไม่ใช้ช่องว่างระหว่างคำ
เครื่องมือเบราว์เซอร์นี้ใช้งานได้กับข้อความที่วางเท่านั้น หากต้องการนับคำในไฟล์จาก command line ให้ใช้ wc -w filename บน Linux หรือ macOS บน Windows PowerShell ให้ใช้ (Get-Content file.txt | Measure-Object -Word).Words สำหรับไฟล์ขนาดใหญ่หรือการประมวลผลหลายไฟล์ เครื่องมือ command-line มีความเร็วกว่าเครื่องมือที่ใช้งานผ่านเบราว์เซอร์
เครื่องมือนับตัวอักษร Unicode ได้ถูกต้องหรือไม่?
ได้ JavaScript ใช้ string.length นับ UTF-16 code units ไม่ใช่ code points ดังนั้นอีโมจิตัวเดียว เช่น ธงชาติ (ซึ่งเป็น ZWJ sequence ของหลาย code points) อาจรายงานจำนวนตัวอักษรสูงกว่าที่คาดไว้ เครื่องมือนี้ใช้วิธีการนับเดียวกับ string API ที่มีอยู่ในเบราว์เซอร์ สำหรับการนับกลุ่มกราฟีมที่แม่นยำ ให้ใช้ Intl.Segmenter API ที่มีในเบราว์เซอร์สมัยใหม่
เครื่องมือนี้เปรียบเทียบกับเครื่องนับคำใน Microsoft Word หรือ Google Docs อย่างไร?
Microsoft Word และ Google Docs ใช้การแยกตามช่องว่างในลักษณะเดียวกันสำหรับการนับคำภาษาอังกฤษ ความแตกต่างเล็กน้อยอาจเกิดขึ้นกับคำที่ใช้ยัติภังค์ em dash ที่ไม่มีช่องว่าง และวิธีที่เชิงอรรถหรือหัวเรื่องถูกรวมเข้าด้วย เครื่องมือนี้นับเฉพาะข้อความที่คุณวาง โดยไม่มีข้อมูลเมตา หัวเรื่อง หรือเชิงอรรถ หากต้องการให้ตรงกับการนับของแพลตฟอร์มเฉพาะ ให้วางข้อความเดียวกันในทั้งสองและเปรียบเทียบ