前端之家收集整理的这篇文章主要介绍了
[VB.NET]求救.速度.!帮忙写点东西.!,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
求救.速度.!帮忙写点东西.! 编写一个
方法,
统计输入的英语字符串有多少个英文单词组成.假设输入的字符串只用空格,逗号,句号分隔单词.
方法的原形如下: 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 __________________________________________________________________________ 学习了,看来正则表达式还是好使,呵呵。 __________________________________________________________________________ 正则表达式效率最好 __________________________________________________________________________ 考虑用正则表达式来实现 __________________________________________________________________________