我正忙于处理过去的试卷,为 Visual Basic 考试做准备。我需要帮助解决我遇到的以下问题。
Write a function procedure to calculate the number of times the characters "e", "f" and "g" appears in a string
我尝试编写伪代码并提出以下内容。
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
如何遍历字符串中的单个字符(使用
for
循环)以及如何计算特定字符在字符串中出现的次数?
最佳答案
答案很大程度上取决于您在类(class)作业中已经学到了什么以及您应该使用哪些功能。
但总的来说,遍历字符串中的字符就像这样简单:
Dim s As String = "test"
For Each c As Char in s
' Count c
Next
至于计数,只需为每个字符设置单独的计数器变量(
eCount As Integer
等),并在 c
时递增它们。等于那个字符——显然,一旦你增加要计算的字符数,这种方法就不能很好地扩展。这可以通过维护相关字符的字典来解决,但我猜这对于你的练习来说太高级了。
关于vb.net - 在 VB.NET 中循环遍历字符串中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13251344/