[VB.NET]求救.速度.!帮忙写点东西.!

来源:互联网 发布:淘宝店铺转让 编辑:程序博客网 时间:2024/04/28 16:23
VB.NET源码-156个实用实例哦……<script type="text/javascript"><!--google_ad_client = "pub-8333940862668978";/* 728x90, 创建于 08-11-30 */google_ad_slot = "4485230109";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
求救.速度.!帮忙写点东西.!
编写一个方法,统计输入的英语字符串有多少个英文单词组成.假设输入的字符串只用空格,逗号,句号分隔单词.方法的原形如下:
Public Function CountWords(ByVal input As String) As Integer

End Function
__________________________________________________________________________
Public Function CountWords(ByVal input As String) As Integer
Dim num As Int32 = 0
input = input.TrimStart( " ")
input = input.TrimEnd( " ")
input = input.Replace(vbCrLf, " ")
Dim sentences() As String = input.Split( ". ")
For Each sentence As String In sentences
If sentence.Length > 0 Then
sentence = sentence.TrimStart( " ")
sentence = sentence.TrimEnd( " ")
Dim clauses() As String = sentence.Split( ", ")
For Each clause As String In clauses
If clause.Length > 0 Then
clause = clause.TrimStart( " ")
clause = clause.TrimEnd( " ")
num += clause.Split( " ").GetUpperBound(0) + 1
End If
Next
End If
Next
Return num
End Function
__________________________________________________________________________
写得有点匆忙,只测试了几个句子,lz看能不能用吧。我是假设句子中不会出现换行符,换行符仅会出现在一句句子之后来做的。
__________________________________________________________________________
Public Function CountWords(ByVal input As String) As Integer
dim re as new Regex( "/w+ ")
dim mc as MatchCollection = re.Matches(input)
return mc.Count
End Function
__________________________________________________________________________
学习了,看来正则表达式还是好使,呵呵。
__________________________________________________________________________
正则表达式效率最好
__________________________________________________________________________
考虑用正则表达式来实现
__________________________________________________________________________
原创粉丝点击