统计字符串中的单词个数
(作者: 2000年04月29日 11:27)
[程序语言] Microsoft Visual Basic 4.0,5.0,6.0
[运行平台] WINDOWS
[源码来源] http://www.codeguru.com/
[功能描述] 该小程序演示了如何返回一个字符串中的单词个数(包括回车和换行),该函数把初始串分解成一行行的子串,并把每行传给函数“countwordsinline”,以便先统计出每行的单词个数。
public Function countwordsinline(byval sourceline as string) as Long
Dim nextspaceindex as Long
sourceline = sourceline & " "
'Every word has space at its end
nextspaceindex = InStr(sourceline, " ")
While nextspaceindex <> 0
If nextspaceindex <> 1 then countwordsinline = countwordsinline + 1
sourceline = mid$(sourceline, nextspaceindex + 1)
nextspaceindex = InStr(sourceline, " ")
Wend
End Function
public Function countwords(byval source as string) as Long
Dim endindex as Long
source = source & Chr$(13)
endindex = InStr(source, Chr$(13))
While (endindex <> 0)
countwords = countwords + countwordsinline(mid$(source, 1, endindex - 1))
source = mid$(source, endindex + 2)
'VB vbCrLf contains 2 Characters
endindex = InStr(source, Chr$(13))
Wend
End Function
|