ccidnet广告

新用户注册

赛迪社区

帮助

  新闻中心 | 关注 | 技术天地 | 软件特供 | IT财经 | 市场专家 | 互动学校 | DIY专区 | 新游戏客栈 | 媒体全文



相关文章








 当前页面位置: 主页: 技术天地: 模板源码: 技术文章

统计字符串中的单词个数
(作者: 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



与CCIDNET联系
webmaster@ciw.com.cn