Vb6 我如何将此代码更改为"&引用;找到前面的单词“quot&引用;在文本框中?

Vb6 我如何将此代码更改为"&引用;找到前面的单词“quot&引用;在文本框中?,vb6,Vb6,当我试图让程序在文本中查找任何单词时 我使用下面的代码来查找单词并选择它,然后查找下一个单词,但我不知道如何编写代码来生成“上一个单词” 如果我有这样一句话:“回家,上学,然后再回家。” …如果我使用前面的代码搜索单词“go”,此代码可以找到单词“go”的第一个匹配项并选择它和下一个匹配项,依此类推。。。 如果我想返回到单词“go”的第一个出现处并选择它..什么是适合这样做的代码?? 我使用的代码是: Private Sub FindText(ByVal start_at As Integer)

当我试图让程序在文本中查找任何单词时

我使用下面的代码来查找单词并选择它,然后查找下一个单词,但我不知道如何编写代码来生成“上一个单词” 如果我有这样一句话:“回家,上学,然后再回家。” …如果我使用前面的代码搜索单词“go”,此代码可以找到单词“go”的第一个匹配项并选择它和下一个匹配项,依此类推。。。 如果我想返回到单词“go”的第一个出现处并选择它..什么是适合这样做的代码?? 我使用的代码是:

Private Sub FindText(ByVal start_at As Integer)

target = Text2.Text
pos = InStr(start_at, " " & Text1.Text & " ", " " & target & " ", vbBinaryCompare)
If pos > 0 Then
    ' We found it.
    Text1.SetFocus
    TargetPosition = pos
    Text1.SelStart = TargetPosition - 1
    Text1.SelLength = Len(target)


Else
    ' We did not find it.
    MsgBox "Not found."
        'Text1.SetFocus
    End If
FindText 1          'search word in text
FindText TargetPosition + 1   ' search the next word

仪表功能

描述

返回一个字符串在另一个字符串中出现的位置(从字符串末尾开始)

语法

InstrRev(stringcheck, stringmatch[, start[, compare]])
StrReverse(expression)
InstrRev函数语法具有以下命名参数:

反向函数

描述

返回指定字符串的字符顺序颠倒的字符串

语法

InstrRev(stringcheck, stringmatch[, start[, compare]])
StrReverse(expression)

我尝试使用strReverse函数,但是发现字符串颠倒了,然后在字符串中找到了正确的索引,这让我很困惑,所以我使用了一个只使用蛮力搜索字符串的函数重写了答案

函数从要搜索的字符串的左边一直搜索到开始位置,因为我们从开始向后搜索,所以不关心右边的任何内容。然后,它从片段中提取一个子字符串,并运行Instr函数以查看是否找到匹配项。如果未找到匹配项,则Mid$函数的开始索引为st如果大于1,则开始索引将递减1,并再次尝试Instr函数

Private Enum SearchDirection
    Forward = 0
    Backwards = 1
End Enum

Private Sub Command1_Click()

    SearchForString CInt(Text1.SelStart + Text1.SelLength), Check1.Value

End Sub

Private Sub SearchForString(ByVal vStart As Integer, Optional Direction As SearchDirection = Forward)
    Dim intIndex As Integer     ' receives return value of InStr function
    Dim intStart As Integer     ' the start parm for the Instr function
    Dim strToSearch As String   ' the string1 parm for the Instr function
    Dim strToFind As String     ' the string2 parm for the Instr function

    strToSearch = Text1.Text
    strToFind = Text2.Text
    intStart = vStart

    If Direction = Forward Then
        Text1.SelStart = Len(strToSearch)
        Text1.SelLength = 0
        If intStart >= Len(strToSearch) Or vStart = 0 Then ' reset the start and begin the search again
            intStart = 1
        End If
        intIndex = InStr(intStart, strToSearch, strToFind, vbBinaryCompare)
    Else
        intIndex = FindPrevious(vStart - Len(strToFind))
    End If

    If intIndex > 0 Then
        Text1.SelStart = intIndex - 1
        Text1.SelLength = Len(strToFind)
        Text1.SetFocus
    Else
        Text1.SelStart = Len(strToSearch)
        Text1.SelLength = 0
    End If

End Sub

Private Function FindPrevious(ByVal vStart As Integer) As Integer
    Dim strFragment As String
    Dim intStart As Integer
    Dim intIndex As Integer

    If vStart > 0 Then
        intStart = vStart
    Else
        intStart = Len(Text1.Text)
    End If

    strFragment = Left$(Text1.Text, intStart)
    Do While intStart > 1 And intIndex = 0
        intStart = intStart - 1
        intIndex = InStr(Mid$(strFragment, intStart), Text2.Text)
    Loop
    FindPrevious = intStart

End Function

此代码取决于Text1的SelStart属性。若要使用此示例,请创建一个新项目并将代码添加到form1代码中。此代码将在字符串上重复循环。如果要显示消息,则需要修改它。错误处理由您决定。

我想您不理解我的问题,我的意思是如果我有这样一句话:“回家,上学,然后再回家。。。如果我使用前面的代码搜索单词“go”,这段代码可以找到单词“go”的第一个匹配项并选择它和下一个匹配项,依此类推。。。如果我想返回到单词“go”的第一个匹配项并选择它..什么是合适的代码来执行此操作?????????????????????????您反转正在搜索的字符串,并从您所在的位置反转搜索字符串。我听不懂您的回答。。如果可以编辑它使之成为我想要实现的,如果向后搜索cat,你必须搜索tac。把这个代码放在程序中测试它,因为我不能测试it@Ibrahim如果要在程序中测试此代码,请创建一个新项目,并在form1中添加两个文本框、一个复选框和一个commandbutton。然后复制上面的代码并粘贴到表单中。键入一些文本以搜索文本1,然后键入一些文本以查找文本2,然后单击按钮。选中复选框以向后搜索。如果您不想这样做,请看我是如何反转字符串和操作索引的。@Ibrahim我修改了我的答案,使用一个单独的函数来搜索字符串以查找上一个匹配项。这应该更容易阅读。