vb.net 计算字符串中的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16604179/
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 13:37:01  来源:igfitidea点击:

Counting the Words in a String

vb.net

提问by Programming Noob

I'm taking an introductory programming class that goes over Visual Basic and I need some help with an assignment if you all would be so kind as to point me in the right direction. It's sort of a challenge assignment and I'm supposed to figure out how to do these things by myself but I haven't been able to find anything on this.

我正在学习一门关于 Visual Basic 的介绍性编程课程,如果你们愿意为我指明正确的方向,我需要一些关于作业的帮助。这是一项具有挑战性的任务,我应该弄清楚如何自己做这些事情,但我一直无法找到任何关于此的信息。

What I have to do is have a program count the number of words in a string and then print this number. I've look online to no avail, and I haven't learned how to do anything similar in class. Any help that you all could offer me would be greatly appreciated. I know you don't like homework assignments, but I am truly lost on this one.

我要做的是让一个程序计算字符串中的单词数,然后打印这个数字。我在网上看无济于事,我还没有学会如何在课堂上做类似的事情。你们可以为我提供的任何帮助将不胜感激。我知道你不喜欢家庭作业,但我真的迷失在这个作业上。

回答by Bathsheba

A starting point, in VBA at least (and VB has similar functions but I don't know which version you're using), is to use VBA.Split; e.g.

一个起点,至少在 VBA 中(和 VB 有类似的功能,但我不知道你使用的是哪个版本),是使用 VBA.Split;例如

VBA.Split(s, " ") where s is the string to split and " " the delimiter (we're assuming, crudely, that all words are separated by spaces).

VBA.Split(s, " ") 其中 s 是要拆分的字符串,而 " " 是分隔符(我们粗略地假设所有单词都用空格分隔)。

This function returns a string array. You can use the UBound and LBound functions to get the size of the array. E.g.

此函数返回一个字符串数组。您可以使用 UBound 和 LBound 函数来获取数组的大小。例如

Sub test()
    Dim s As String
    Dim arr() As String

    arr = VBA.Split("Hello there", " ")

    Debug.Print UBound(arr) - LBound(arr) + 1

End Sub

This will print 2.

这将打印 2。

Happy coding!

快乐编码!

回答by Placeholder

OK, let us say your string is like:

好的,让我们说你的字符串是这样的:

string ThisIsYourSentence = "This is some pretty long and annoying sentence, now, isn't it?";

You notice that each word is divided from the other by an interval " ". In C# there is a built-in method which do separate strings inside a string, by checking for a special character which divides them:

您会注意到每个单词都被间隔“ ”分开。在 C# 中,有一个内置方法可以在字符串中分离字符串,通过检查将它们分开的特殊字符:

For your solution you just have to create a new array, which each time when the " " character is met, will fill in with a word.

对于您的解决方案,您只需要创建一个新数组,每次遇到“”字符时,都会用一个单词填充。

string[] TheseAreYourWords = ThisIsYourSentence.Split(' ');

P.S: Oh, sorry, I didn't even see you meant VisualBasic, not C#.

PS:哦,抱歉,我什至没有看到您指的是 VisualBasic,而不是 C#。

回答by Kuzgun

you can call a function like this:

你可以调用这样的函数:

Public Function CountWords(ByVal value As String) As Integer
    ' Count matches.
    Dim collection As MatchCollection = Regex.Matches(value, "\S+")
    Return collection.Count
    End Function

or split the given string with space character and get length of array.

或者用空格字符分割给定的字符串并获取数组的长度。