vb.net 如何计算字符串中的单词数?网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20376198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:00:58 来源:igfitidea点击:
How to count the number of words in a string? vb.net
提问by Conor C
Hey guys I need some help. What expression do I use to count the number of words in a user-inputted string in Visual Basic? It needs to be done without making it an array.
嘿伙计们,我需要一些帮助。在 Visual Basic 中,我使用什么表达式来计算用户输入字符串中的单词数?它需要在不使其成为数组的情况下完成。
回答by Jadeja RJ
Public Function CountWords(ByVal value As String) As Integer
' Count matches.
Dim collection As MatchCollection = Regex.Matches(value, "\S+")
Return collection.Count
End Function
回答by liquidsnake786
Dim testString = "this is a test string with 8 words."
Dim counter = 1
For Each testCharacter In testString
If (String.IsNullOrWhiteSpace(testCharacter)) Then
counter += 1
End If
Next

