字符统计功能模块


复制代码 代码如下:

Sub TongJi()

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objFile = objFSO.OpenTextFile(“ok.txt”, 1)

strText = objFile.ReadAll

All = Len(strText)

S=”` ~ ! @ # $ % ^ & * ( ) – _ = + \ | [ { ] } ; : ‘ , < . > / ? ・ ! ¥ ( ) 、 【 】 ; : ‘ ‘ “ ” , 《 。 》 ? …… ―― chr(34) vbCrLf”

Signs=Split(S,” “)

For Each Sign in Signs

strText = Replace(strText, “Sign”, ” “)

Next

objFile.Close

For k=1 To Len(strText)

tmpStr=Mid(strText, k, 1)

If Asc(tmpStr) > 127 Or Asc(tmpStr) < 0 Then

c = c + 1

tmpStr=” ”

End If

Str = Str + tmpStr

Next

arrWords = Split(Str, ” “)

For Each strWord in arrWords

If Len(strWord) > 0 Then

i = i + 1

End If

Next

Sum = c + i

msgbox “字符总数:” & All & chr(13) & “有效字符:” & Sum & chr(13) & “汉字总数:” & c, 64, “字符统计”

End sub

TongJi()将上面代码保存为TongJi.vbs,在TongJi.vbs同目录下建立ok.txt文本文件,将你要统计的文本复制到ok.txt中,运行TongJi.vbs即可以统计字数,与word的功能一样。不过算法不一样,结果也不太一样,原因是如何理解何为“有效字符”

声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。