string 在 VB.NET 中循环遍历字符串中的字符

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

Looping through characters in a string in VB.NET

vb.netstringloopscharacter

提问by Timothy Coetzee

I'm busy working on past exam papers in preparation for a Visual Basic exam. I need help with the following question which I'm stuck with.

我正忙于编写过去的试卷,为 Visual Basic 考试做准备。我需要帮助解决以下我遇到的问题。

Write a function procedure to calculate the number of times the characters "e", "f" and "g" appears in a string

编写函数程序计算字符串中字符“e”、“f”和“g”出现的次数

I tried to write the psuedo code and came up with the following.

我尝试编写伪代码并提出以下内容。

Loop through each individual character in the string
If the character = "e","f" or "g" add 1 to number of characters
Exit loop 
Display total in messagebox

How do I loop through individual characters in a string (using a forloop) and how do I count the number of times a specific character appears in a string?

如何遍历字符串中的单个字符(使用for循环)以及如何计算特定字符在字符串中出现的次数?

采纳答案by Konrad Rudolph

The answer greatly depends on what you've already learned in your course work and which functions you are supposed to use.

答案很大程度上取决于您在课程作业中已经学到了什么以及您应该使用哪些功能。

But in general, looping over the characters in a string is as easy as this:

但总的来说,遍历字符串中的字符就像这样简单:

Dim s As String = "test"

For Each c As Char in s
    ' Count c
Next

As for counting, simply have separate counter variables (eCount As Integeretc.) for each character and increment them when cequals that character?–?obviously that approach doesn't scale well once you increase the number of characters to count. This can be solved by maintaining a dictionary of the relevant characters but I'm guessing that this is too advanced for your exercise.

至于计数,只需eCount As Integer为每个字符设置单独的计数器变量(等),并在c等于该字符时增加它们?–?显然,一旦增加要计数的字符数,这种方法就不能很好地扩展。这可以通过维护相关字符的字典来解决,但我猜这对您的练习来说太高级了。

回答by Christian Sauer

Looping through a string is simple: a string can be treated as a list of characters, which can be looped.

循环遍历一个字符串很简单:一个字符串可以被视为一个字符列表,它可以被循环。

Dim TestString = "ABCDEFGH"
for i = 0 to TestString.length-1
debug.print(teststring(i))
next

even easier would be a for..each loop, but sometimes a for i loop is better

更容易的是 for..each 循环,但有时 for i 循环更好

For counting the numbers I would use a dictionary Like this:

为了计算数字,我会使用这样的字典:

        Dim dict As New Dictionary(Of Char, Integer)
        dict.Add("e"c, 0)
Beware: a dictionary can only hold ONE item of the key - that means, adding another "e" would cause an error.
each time you encounter the char you want, call something like this:
        dict.Item("e"c) += 1

回答by Tim Schmelter

If you're allowed to use (or you want to learn) Linq, you can use Enumerable.GroupBy.

如果你被允许使用(或者你想学习)Linq,你可以使用Enumerable.GroupBy.

Assuming your question is the text you want to search:

假设您的问题是您要搜索的文本:

Dim text = "H*ow do i loop through individual characters in a string (using a for loop) and how do I count the number of times a specific character appears in a string?*"
Dim charGroups = From chr In text Group By chr Into Group

Dim eCount As Int32 = charGroups.Where(Function(g) g.chr = "e"c).Sum(Function(g) g.Group.Count)
Dim fCount As Int32 = charGroups.Where(Function(g) g.chr = "f"c).Sum(Function(g) g.Group.Count)
Dim gCount As Int32 = charGroups.Where(Function(g) g.chr = "g"c).Sum(Function(g) g.Group.Count)