统计字数
要准确地统计字数,可逐一将字符串转换为ASCII码,依据其值判断是为中文字符还是英文字符。0——127之间的为大小写字母及数字、半角标点符号、回车、换行等,中文字符的ASCII值则不在0——127之列了。这样,纯汉字的字数统计是很容易的,倒是英文的统计复杂,因为英文统计应以单词为单位,而要判断是否为单词并不是简单的事。我们可以这么处理:如果被检测的字符为大小写字母,则判断其后面的字符是否为一个单词的标志(如空格、标点符号、回车符等),如是,则判断为一个单词。
以下代码能较准确地统计出TextBox控件中的中、英文字数和数字字符数,并将全部字节数(含各种控制符如回车等)也统计出来。适用于中英文编排环境。
'窗体级声明
Dim c As Long, e_word As Long '中英文字数
Dim Num As Long, s As Long '数字及全部字符数
'统计——
Private Sub Command1_Click()
Dim Str As String '总字符
Dim k As Long '计数器
Dim tmpStr As String '逐一检测的字符
c = 0: e_word = 0: Num = 0: s = 0 '清空变量
Str = Text1.Text & " " '加一空格便于意外时计算最后一个字符
For k = 1 To Len(Str) - 1
tmpStr = Mid$(Str, k, 1)
If Asc(tmpStr) >= 65 And Asc(tmpStr) <= 90 Then '小写字母
If Asc(Mid$(Str, k + 1, 1)) <= 64 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 90 And Asc(Mid$(Str, k + 1, 1)) < 97 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 122 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) = 39 Or Asc(Mid$(Str, k + 1, 1)) = 45 Then e_word = e_word - 1 '是符号'或-时
ElseIf Asc(tmpStr) >= 97 And Asc(tmpStr) <= 122 Then '大写字母
If Asc(Mid$(Str, k + 1, 1)) < 65 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 90 And Asc(Mid$(Str, k + 1, 1)) < 97 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 122 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) = 39 Or Asc(Mid$(Str, k + 1, 1)) = 45 Then e_word = e_word - 1 '是符号'或-时
ElseIf Asc(tmpStr) >= 48 And Asc(tmpStr) <= 57 Then '阿拉伯数字数字
If Asc(Mid$(Str, k + 1, 1)) < 48 Or Asc(Mid$(Str, k + 1, 1)) > 57 Then Num = Num + 1
ElseIf Asc(tmpStr) > 127 Or Asc(tmpStr) < 0 Then '中文字符
c = c + 1
End If
Next
s = LenB(StrConv(Text1.Text, vbFromUnicode)) '全部字符
MsgBox "本文共有:" & vbCrLf & vbCrLf & "汉字字数: " & c & _
" 个 (含全角标点)" & vbCrLf & "英文单词: " & e_word & " 个 (不含半角标点)" & vbCrLf & _
"数字: " & Num & " 个" & vbCrLf & "全部字节: " & s & " 个", vbInformation, "字数统计"
以下代码能较准确地统计出TextBox控件中的中、英文字数和数字字符数,并将全部字节数(含各种控制符如回车等)也统计出来。适用于中英文编排环境。
'窗体级声明
Dim c As Long, e_word As Long '中英文字数
Dim Num As Long, s As Long '数字及全部字符数
'统计——
Private Sub Command1_Click()
Dim Str As String '总字符
Dim k As Long '计数器
Dim tmpStr As String '逐一检测的字符
c = 0: e_word = 0: Num = 0: s = 0 '清空变量
Str = Text1.Text & " " '加一空格便于意外时计算最后一个字符
For k = 1 To Len(Str) - 1
tmpStr = Mid$(Str, k, 1)
If Asc(tmpStr) >= 65 And Asc(tmpStr) <= 90 Then '小写字母
If Asc(Mid$(Str, k + 1, 1)) <= 64 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 90 And Asc(Mid$(Str, k + 1, 1)) < 97 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 122 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) = 39 Or Asc(Mid$(Str, k + 1, 1)) = 45 Then e_word = e_word - 1 '是符号'或-时
ElseIf Asc(tmpStr) >= 97 And Asc(tmpStr) <= 122 Then '大写字母
If Asc(Mid$(Str, k + 1, 1)) < 65 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 90 And Asc(Mid$(Str, k + 1, 1)) < 97 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) > 122 Then e_word = e_word + 1
If Asc(Mid$(Str, k + 1, 1)) = 39 Or Asc(Mid$(Str, k + 1, 1)) = 45 Then e_word = e_word - 1 '是符号'或-时
ElseIf Asc(tmpStr) >= 48 And Asc(tmpStr) <= 57 Then '阿拉伯数字数字
If Asc(Mid$(Str, k + 1, 1)) < 48 Or Asc(Mid$(Str, k + 1, 1)) > 57 Then Num = Num + 1
ElseIf Asc(tmpStr) > 127 Or Asc(tmpStr) < 0 Then '中文字符
c = c + 1
End If
Next
s = LenB(StrConv(Text1.Text, vbFromUnicode)) '全部字符
MsgBox "本文共有:" & vbCrLf & vbCrLf & "汉字字数: " & c & _
" 个 (含全角标点)" & vbCrLf & "英文单词: " & e_word & " 个 (不含半角标点)" & vbCrLf & _
"数字: " & Num & " 个" & vbCrLf & "全部字节: " & s & " 个", vbInformation, "字数统计"