x 重复字符的索引

发布于 2024-07-15 18:07:02 字数 500 浏览 8 评论 0原文

以下代码应该在消息框中显示字符串 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白龙吟 2024-07-22 18:07:02

我做错了什么?

你正在寻找第二个a。 我猜你在想这个:

checkme.IndexOf("a", checkme.IndexOf("a", 3))

这实际上会给你正确的结果。 (它说“查找第一个a出现在第3个字符(恰好是a)上或之后出现的第一个a”)

您的原始代码说“查找出现在超出第3个字符或之后3个位置的第一个a”第一个 a",只会到达第二个 a

如何解决这个问题?

可以在循环中使用IndexOf,将最后找到的索引重新用作下一个起始索引。

Shared Public Function FindIndexOfNthChar(ByVal checkme as String, _
                                          ByVal checkChar as Char, _
                                          ByVal n as Integer) as Integer
   Dim lastIndex As Integer = -1

   For i As Integer = 1 To n
      lastIndex = checkme.IndexOf(checkChar, lastIndex + 1)
      If lastIndex = -1 Then Return -1
   Next i

   Return lastIndex
End Function

你必须要小心; 如果您不在每次尝试时检查 -1 并立即退出,则最终可能会得到错误的结果。

如果您忘记了这一点(就像其他一些帖子似乎那样),那么如果您在带有单个 a 的字符串中搜索第三个 a ,您实际上会返回第一个 a 的索引(当您尝试查找第二个 a 时,您会将索引重置为 -1,这实际上会重新开始搜索)

因此,准确地写出你的意思可能会更清楚:

Shared Public Function FindIndexOfNthChar(ByVal checkme as String, _
                                   ByVal checkChar as Char, _ 
                                   ByVal n as Integer) as Integer
   Dim count as Integer = 0

   For i as Integer = 0 To checkme.Length - 1
      If(checkme(i) = checkChar) Then
         count += 1
         If(count = n) Then Return i 
      End If
   Next i

   return -1
End Function

What am I doing wrong?

You're finding the second a. I'm guessing you were thinking of this:

checkme.IndexOf("a", checkme.IndexOf("a", 3))

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.

Shared Public Function FindIndexOfNthChar(ByVal checkme as String, _
                                          ByVal checkChar as Char, _
                                          ByVal n as Integer) as Integer
   Dim lastIndex As Integer = -1

   For i As Integer = 1 To n
      lastIndex = checkme.IndexOf(checkChar, lastIndex + 1)
      If lastIndex = -1 Then Return -1
   Next i

   Return lastIndex
End Function

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 single a you'll actually return the index of the first a (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:

Shared Public Function FindIndexOfNthChar(ByVal checkme as String, _
                                   ByVal checkChar as Char, _ 
                                   ByVal n as Integer) as Integer
   Dim count as Integer = 0

   For i as Integer = 0 To checkme.Length - 1
      If(checkme(i) = checkChar) Then
         count += 1
         If(count = n) Then Return i 
      End If
   Next i

   return -1
End Function
七颜 2024-07-22 18:07:02

你只找到了第二个“a”。 尝试使用这样的循环。

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 = -1
    For i As Integer = 1 To 3
        indexnum = checkme.IndexOf("a", indexnum + 1)
    Next
    MessageBox.Show(CStr(indexnum))
End Sub

You're only finding the second "a". Try using a loop like so.

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 = -1
    For i As Integer = 1 To 3
        indexnum = checkme.IndexOf("a", indexnum + 1)
    Next
    MessageBox.Show(CStr(indexnum))
End Sub
木格 2024-07-22 18:07:02

checkme.IndexOf("a") 是 2。加上 3 得到 5。所以整个表达式变成:

indexnum = checkme.IndexOf("a", 5)

找到第二个。

如果要找到第 N 个,则循环 N 次,每次循环时执行以下操作:

pos = checkme.IndexOf('a', pos + 1)

在循环之前,将 pos 设置为 -1。

checkme.IndexOf("a") is 2. Adding 3 gives 5. So the whole expression becomes:

indexnum = checkme.IndexOf("a", 5)

Which finds the second one.

If you want to find the Nth one, loop N times, and each time round the loop do:

pos = checkme.IndexOf('a', pos + 1)

Before the loop, set pos to be -1.

痞味浪人 2024-07-22 18:07:02

您拥有的代码不会产生预期的效果。 相反,它所做的是查找位置 2 处的第一个“a”。然后查找位置 5 (2+3) 之后出现的第一个“a”。 如果您想要第三个“a”,您可以使用类似于以下内容的内容

indexnum = checkme.IndexOf("a")
indexnum = checkme.IndexOf("a", indexnum+1)
indexnum = checkme.IndexOf("a", indexnum+1)

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

indexnum = checkme.IndexOf("a")
indexnum = checkme.IndexOf("a", indexnum+1)
indexnum = checkme.IndexOf("a", indexnum+1)
拥醉 2024-07-22 18:07:02

为什么会是12? 位置 5 之后的下一个“a”位于位置 9。您的代码基本上可以分解为:

'thais is a sample...
'0123456789
Dim idxFirstA = checkme.IndexOf("a") ' equals 2

'thais is a sample...
'0123456789
Dim idxThirdA = checkme.IndexOf("a", 2 + 3) ' equals 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:

'thais is a sample...
'0123456789
Dim idxFirstA = checkme.IndexOf("a") ' equals 2

'thais is a sample...
'0123456789
Dim idxThirdA = checkme.IndexOf("a", 2 + 3) ' equals 9

You skip the 1st "a" (position 2), and then find the 2nd "a" (position 9).

伏妖词 2024-07-22 18:07:02
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 = -1, occrnc As Integer = 0
    For x As Integer = 0 To checkme.Length - 1
        indexnum = checkme.IndexOf("a", indexnum + 1)
        If indexnum <> -1 Then occrnc += 1
        If occrnc = 3 Then Exit For
    Next
    If occrnc = 3 Then MessageBox.Show(indexnum.ToString)
End Sub
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 = -1, occrnc As Integer = 0
    For x As Integer = 0 To checkme.Length - 1
        indexnum = checkme.IndexOf("a", indexnum + 1)
        If indexnum <> -1 Then occrnc += 1
        If occrnc = 3 Then Exit For
    Next
    If occrnc = 3 Then MessageBox.Show(indexnum.ToString)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文