[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 __________________________________________________________________________ 学习了,看来正则表达式还是好使,呵呵。 __________________________________________________________________________ 正则表达式效率最好 __________________________________________________________________________ 考虑用正则表达式来实现 __________________________________________________________________________

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...