编程高手快来啊!!!!谁会编不重复非中文单词数(英文单词)统计啊!

来源:百度知道 编辑:UC知道 时间:2024/05/31 16:46:20
这个可以参考啊http://zhidao.baidu.com/question/19239702.html?si=1

我要的是英文单词数.

英文单词和汉字不一样,汉字是定长的,用mid就可以取出,而英文需要要根据单词边界来划分,最简单的办法是用正则表达式,代码如下:
Function countEWords(strIn As String)
'这个是主要统计过程,输出strIn字符串中包含的不重复英文单词数
Dim strAll As String
Dim strCol As String
Dim iCount As Long
Set regEx = CreateObject("VBScript.RegExp")

With regEx
.Pattern = "\b[a-zA-Z]+\b"
.Global = True
.IgnoreCase = True
Set matches = .Execute(strIn)
End With
strCol = " "
For i = 0 To matches.Count - 1
If InStr(1, strCol, " " & matches(i).Value & " ", vbTextCompare) = 0 Then
iCount = iCount + 1
strCol = strCol & matches(i).Value & " "
End If
Next
countEWords = iCount
End Function

Sub test()
MsgBox countEWords("one and two and three 加起来是six") '这句应返回5,即5个英文单词:one and