专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »VB教程 » 在线字数统计:统计字数 »正文

在线字数统计:统计字数

来源: 发布时间:星期四, 2009年2月12日 浏览:32次 评论:0


要准确地统计字数可逐串转换为ascii码依据其值判断是为中文还是英文0——127的间为大小写字母及数字、半角标点符号、回车、换行等中文ascii值则不在0——127的列了这样纯汉字字数统计是很容易倒是英文统计复杂英文统计应以单词为单位而要判断是否为单词并不是简单我们可以这么处理:如果被检测为大小写字母则判断其后面是否为个单词标志(如空格、标点符号、回车符等)如是则判断为个单词
以下代码能较准确地统计出textboxControl控件中中、英文字数和数字并将全部字节数(含各种控制符如回车等)也统计出来适用于中英文编排环境

\'窗体级声明
dim c as long, e_word as long \'中英文字数
dim num as long, s as long \'数字及全部

\'统计——
private sub command1_click

dim str as \'总
dim k as long \'计数器
dim tmpstr as \'逐检测

c = 0: e_word = 0: num = 0: s = 0 \'清空变量
str = text1.text & \" \" \'加空格便于意外时计算最后
for k = 1 to len(str) - 1
tmpstr = mid$(str, k, 1)

asc(tmpstr) >= 65 and asc(tmpstr) <= 90 then \'小写字母
asc(mid$(str, k + 1, 1)) <= 64 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) > 90 and asc(mid$(str, k + 1, 1)) < 97 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) > 122 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) = 39 or asc(mid$(str, k + 1, 1)) = 45 then e_word = e_word - 1 \'是符号\'或-时
asc(tmpstr) >= 97 and asc(tmpstr) <= 122 then \'大写字母
asc(mid$(str, k + 1, 1)) < 65 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) > 90 and asc(mid$(str, k + 1, 1)) < 97 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) > 122 then e_word = e_word + 1
asc(mid$(str, k + 1, 1)) = 39 or asc(mid$(str, k + 1, 1)) = 45 then e_word = e_word - 1 \'是符号\'或-时
asc(tmpstr) >= 48 and asc(tmpstr) <= 57 then \'阿拉伯数字数字
asc(mid$(str, k + 1, 1)) < 48 or asc(mid$(str, k + 1, 1)) > 57 then num = num + 1
asc(tmpstr) > 127 or asc(tmpstr) < 0 then \'中文
c = c + 1
end
next

s = lenb(strconv(text1.text, vbfromunicode)) \'全部

msgbox \"本文共有:\" & vbcrlf & vbcrlf & \"汉字字数: \" & c & _
\" 个 (含全角标点)\" & vbcrlf & \"英文单词: \" & e_word & \" 个 (不含半角标点)\" & vbcrlf & _
\"数字: \" & num & \" 个\" & vbcrlf & \"全部字节: \" & s & \" 个\", vbinformation, \"字数统计\"

end sub
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: