【问题标题】:Search for an position of a string搜索字符串的位置
【发布时间】:2013-06-07 16:31:19
【问题描述】:

我正在使用 VB 2010,我有一个小问题。好吧,我的 RichTextBox 需要一个特殊的搜索功能。

我的 RichTextBox1 中有一个很长的字符串。我在那里搜索了一个特定的值:

 Dim firstposition As String = CStr(CDbl(TextBox6.Text) * 8 - 7)
    Dim valueofadress = Mid(RichTextBox3.Text, CInt(firstposition), 8)
    TextBox5.Text = valueofadress

    Dim regi1 = Mid(RichTextBox3.Text, 1, CInt(CDbl(TextBox6.Text) * 8))
    Dim t = Split(regi1, tempcode)
    Dim result = UBound(t)

    TextBox4.Text = result.ToString
    TextBox7.Text = regi1.ToString

这不是问题。我有第二个带有 similar 字符串的 RichTextBox。现在我想在这个 rtb 中搜索一个存在超过 10000 次的字符串。我想找出那个字符串的位置。我也有一个计数器,它给了我正在搜索的位置的字符串的编号。

这是我的柜台:

Dim regi1 = Mid(RichTextBox3.Text, 1, CInt(CDbl(TextBox6.Text) * 8))
    Dim t = Split(regi1, tempcode)
    Dim result = UBound(t)

    TextBox4.Text = result.ToString

例子:

如果 counter = 4 并且我想搜索存在 10 次的字符串“Hello world”,那么我想要 RichTextBox 中第 4 个“Hello world”的位置。好吧,我已经尝试过 InStr 和 IndexOf 但没有正确的结果...

【问题讨论】:

    标签: vb.net string textbox


    【解决方案1】:

    您可以尝试String.IndexOf 的重载之一,它允许您指定搜索的起始位置:

    Dim lastFindIndex As Integer = 0
    Dim stringToFind As String = "Find me!"
    Dim currentNumberOfMatches = 0
    Dim matchToStopAt As Integer = 4
    
    While lastFindIndex > -1
    
        currentNumberOfMatches += 1
    
        If matchToStopAt = currentNumberOfMatches Then
    
            Exit While
    
        End If
    
        lastFindIndex = MyTextBox.Text.IndexOf(stringToFind, lastFindIndex)
    
    End While
    

    当循环退出时,您知道在哪里找到了字符串 (lastFindIndex),然后您可以将其输入到您的其他逻辑中。

    【讨论】:

      【解决方案2】:

      我不确定我是否完全理解您的问题,但我认为您最好使用正则表达式来执行此操作。

      类似的东西:

          Dim pattern As String = TextBox6.Text
          Dim m As MatchCollection = Regex.Matches(RichTextBox3.Text, pattern)
      

      现在您可以遍历您的匹配集合 - 每个匹配的位置可以通过 m([]).Index 找到。因此,例如,您想找到匹配项的第 4 次出现,您可以使用 m(3).index

      希望这会有所帮助。

      PS:

      1. 记得在代码顶部使用Imports System.Text.RegularExpressions
      2. 这假设您的 TextBox6.Text 中没有任何特殊字符(可能需要在您的模式中转义)

      【讨论】:

        【解决方案3】:

        我也建议在这里使用正则表达式。我会选择 \b 单词边界和简单的功能:

        Public Function SearchForGivenString(text As String, pattern As String, Optional position As Integer = 1) As Match
            Dim found As MatchCollection = Regex.Matches(text, "\b" & pattern & "\b")
            Dim returnMe As Match
        
            If found.Count = 0 Then
                Return returnMe
            ElseIf position > found.Count Then
                Return found(found.Count - 1)
            Else
                Return found(position - 1)
            End If
        End Function
        

        然后你可以像这样使用它:

            Dim testString As String = "Something really long as a text string. Something really long as a text string. Something really long as a text string. Something really long as a text string. Something really long as a text string. Something really long as a text string. Something really long as a text string."
            Dim returnMatch As Match = SearchForGivenString(testString, "really", 7)
            If Not IsNothing(returnMatch) Then
                Console.WriteLine(returnMatch.Value & " " & returnMatch.Index)
            End If
            Console.ReadLine()
        

        当然你可以发送 Textbox4.Text 和类似的.... 并且有不同的输出。重点在于读取返回匹配的索引。

        【讨论】:

        • 谢谢大家的帮助!但不幸的是,这段代码没有返回任何东西......
        • 当然可以。如果您想要 TextBox3 中的第 4 个“Hello world”,请执行以下操作:Dim myResult as Match = SearchForGivenString(TextBox3.Text, "Hello world", 4)。这个字符串在 TextBox 3 中的位置是:myResult.Index
        猜你喜欢
        • 2022-11-21
        • 1970-01-01
        • 2016-06-26
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-17
        • 1970-01-01
        相关资源
        最近更新 更多