x 重复字符的索引
以下代码应该在消息框中显示字符串 checkme 中的 third a 的索引,但是当我运行该程序时,它没有给我正确的答案(应该是 12,但我得到了 9)。 我做错了什么以及我该如何让它发挥作用?
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim checkme As String = "thais is a sample sentence a"
Dim indexnum As Integer
indexnum = checkme.IndexOf("a", checkme.IndexOf("a") + 3)
MessageBox.Show(CStr(indexnum))
End Sub
选项显式和严格必须打开。 提前致谢。
The following code is supposed to show in a message box the index of the third a in the string checkme, but when I run the program it doesn't give me the right answer (should be 12, instead I get 9). What am I doing wrong and how would I go about making it work?
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim checkme As String = "thais is a sample sentence a"
Dim indexnum As Integer
indexnum = checkme.IndexOf("a", checkme.IndexOf("a") + 3)
MessageBox.Show(CStr(indexnum))
End Sub
Option Explicit and Strict must be On. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我做错了什么?
你正在寻找第二个
a
。 我猜你在想这个:这实际上会给你正确的结果。 (它说“查找第一个a出现在第3个字符(恰好是a)上或之后出现的第一个a”)
您的原始代码说“查找出现在超出第3个字符或之后3个位置的第一个a”第一个 a",只会到达第二个
a
。如何解决这个问题?
您可以在循环中使用
IndexOf
,将最后找到的索引重新用作下一个起始索引。你必须要小心; 如果您不在每次尝试时检查
-1
并立即退出,则最终可能会得到错误的结果。如果您忘记了这一点(就像其他一些帖子似乎那样),那么如果您在带有单个
a
的字符串中搜索第三个a
,您实际上会返回第一个a
的索引(当您尝试查找第二个 a 时,您会将索引重置为-1
,这实际上会重新开始搜索)因此,准确地写出你的意思可能会更清楚:
What am I doing wrong?
You're finding the second
a
. I'm guessing you were thinking of this:which would actually give you the right result. (It says "Find the first a that occurs after the first a that occurs on or after the 3rd character (which happens to be an a)")
Your original code said "Find the first a which occurs on or after 3 positions beyond the first a", which only gets you to the second
a
.How do I fix it?
You could just use the
IndexOf
in a loop, reusing the last found index as the next start index.You have to be careful; if you don't check for
-1
on every attempt and exit immediately, you can end up with wrong results.If you forgot this (as some of the other posts seem to have), then if you search for the third
a
in a string with a singlea
you'll actually return the index of the firsta
(When you try to find the second a, you'll reset your index to-1
, which essentially starts the search over)For that reason, it might be clearer just to write exactly what you mean:
你只找到了第二个“a”。 尝试使用这样的循环。
You're only finding the second "a". Try using a loop like so.
checkme.IndexOf("a")
是 2。加上 3 得到 5。所以整个表达式变成:找到第二个。
如果要找到第 N 个,则循环 N 次,每次循环时执行以下操作:
在循环之前,将 pos 设置为 -1。
checkme.IndexOf("a")
is 2. Adding 3 gives 5. So the whole expression becomes:Which finds the second one.
If you want to find the Nth one, loop N times, and each time round the loop do:
Before the loop, set pos to be -1.
您拥有的代码不会产生预期的效果。 相反,它所做的是查找位置 2 处的第一个“a”。然后查找位置 5 (2+3) 之后出现的第一个“a”。 如果您想要第三个“a”,您可以使用类似于以下内容的内容
The code you have will not produce the desired effect. What it is instead doing is finding the first "a" which is at position 2. then it is finding the first "a" that occurs after position 5 (2+3). If you want the third "a" you could use something similar to the following
为什么会是12? 位置 5 之后的下一个“a”位于位置 9。您的代码基本上可以分解为:
跳过第一个“a”(位置 2),然后找到第二个“a”(位置 9)。
Why would it be 12? The next "a" after position 5 is at position 9. Your code basically breaks down to:
You skip the 1st "a" (position 2), and then find the 2nd "a" (position 9).