什么是字数统计?
字数统计工具可以告诉你一段文本包含多少字词、字符、句子和段落。它以空白字符为边界切分输入内容,并运用基于规则的模式来识别句子和段落。“字词”的定义因语言、文字体系和使用场景而异。在英文中,以空白分隔的词元效果良好。但在中日韩(CJK)语言中,词语之间没有空格,分词需要借助基于词典的算法,例如 ICU 的 BreakIterator。
字符计数有两种常见定义:含空格和不含空格。总字符数包括文本中的每一个 Unicode 码位,包括空格、制表符和换行符。不含空格的字符数会在计数前去除所有空白字符,这是 Twitter(现更名为 X)等平台用于限制发布长度的指标,也是按字符收费的译员所使用的计量方式。在向有严格长度限制的系统粘贴文本时,这两种计数方式的区别至关重要。
阅读时间和朗读时间的估算方式是用字数除以平均速率。发表于《记忆与语言杂志》的研究(Brysbaert,2019 年)表明,英文散文的平均默读速度为每分钟 238 个词。演讲的朗读速度通常为每分钟 130 至 160 个词。这些平均值因文本难度、受众和语言的不同而有所差异,但对于博客文章、技术文档和幻灯片演示来说,已是实用的参考依据。
为什么使用这个字数统计器?
粘贴文本后即可实时获得字数、字符数、句子数和段落数,无需注册账号,也不会向网络发送任何数据。
字数统计器使用场景
文本指标参考
本工具报告的每项指标都有具体的定义。下表说明了各指标的计算方式。
| 指标 | 计算方式 | 示例 |
|---|---|---|
| Words | Sequences separated by whitespace | "hello world" → 2 |
| Characters | All characters including spaces | "hi there" → 8 |
| Characters (no spaces) | Letters, digits, punctuation only | "hi there" → 7 |
| Sentences | Segments ending with . ? or ! | "Hi. Bye!" → 2 |
| Paragraphs | Text blocks separated by blank lines | "A\n\nB" → 2 |
| Reading time | Word count ÷ 238 wpm (silent reading avg) | 1 000 words → ~4.2 min |
| Speaking time | Word count ÷ 150 wpm (presentation pace) | 1 000 words → ~6.7 min |
字数 vs. 字符数
这两项指标回答的问题不同。选错指标可能导致投稿被拒或界面布局出现问题。
代码示例
以下展示如何在不同编程语言中以编程方式统计字数和字符数,每个示例处理相同的输入字符串以便对比。
// 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
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)) # → 2package 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
}# 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