加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 23:44:15 所属栏目:大数据 来源:网络整理
导读:求救.速度.!帮忙写点东西.! 编写一个方法,统计输入的英语字符串有多少个英文单词组成.假设输入的字符串只用空格,逗号,句号分隔单词.方法的原形如下: Public Function CountWords(ByVal input As String) As Integer End Function __________________________
求救.速度.!帮忙写点东西.! 编写一个方法,统计输入的英语字符串有多少个英文单词组成.假设输入的字符串只用空格,逗号,句号分隔单词.方法的原形如下: 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 __________________________________________________________________________ 学习了,看来正则表达式还是好使,呵呵。 __________________________________________________________________________ 正则表达式效率最好 __________________________________________________________________________ 考虑用正则表达式来实现 __________________________________________________________________________

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读