0
   Public Function GetStringBetween(ByVal InputText As String, _
  ByVal starttext As String, _
  ByVal endtext As String)

        Dim lnTextStart As Long
        Dim lnTextEnd As Long

        lnTextStart = InStr(StartPosition, InputText, starttext, vbTextCompare) + Len(starttext)
        lnTextEnd = InStr(lnTextStart, InputText, endtext, vbTextCompare)
        If lnTextStart >= (StartPosition + Len(starttext)) And lnTextEnd > lnTextStart Then
            GetStringBetween = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)
        Else
            GetStringBetween = "ERROR"
        End If
    End Function
    Dim xa As String
        Dim x As String = WebBrowser1.DocumentText

用法

  xa = GetStringBetween(x, TextBox1.Text, TextBox2.Text)

    MsgBox(xa)

我尝试了许多不同的方法来尝试获取其他 2 个字符串之间的所有字符串,每个等该站点有超过 1 个字符串与这 2 个字符串,但我只是得到 2 个字符串之间的第一个字符串帮助抱歉,它很难解释:/

4

3 回答 3

2

编写该代码的最简洁的方法可能是通过 RegEx,但它可能有点矫枉过正。
这是一个使用 string.IndexOf 做同样事情的简单方法

Public Function GetStringBetween(ByVal InputText As String, _
                                 ByVal starttext As String, _
                                 ByVal endtext As String)

    Dim startPos As Integer
    Dim endPos As Integer
    Dim lenStart As Integer
    startPos = InputText.IndexOf(startText, StringComparison.CurrentCultureIgnoreCase)
    if startPos >= 0 Then
        lenStart = startPos + starttext.Length
        endPos = InputText.IndexOf(endtext, lenstart, StringComparison.CurrentCultureIgnoreCase)
        If endPos >= 0 Then
            return InputText.Substring(lenStart, endPos - lenStart)
        End If
    End If
    return "ERROR"
End Function
于 2013-05-06T09:10:24.257 回答
0

您可以使用正则表达式。例如:(word1).*(word2)

于 2013-05-06T09:00:16.230 回答
0
Public Function GetBetween(IStringStr As String, IBefore As String, IPast As String)
    On Error Resume Next
    Dim iString As String
    iString = IStringStr
    iString = Right(iString, Len(iString) - InStr(iString, IBefore) - Len(IBefore) + 1)
    iString = Mid(iString, 1, InStr(iString, IPast) - 1)
    GetBetween = iString
End Function

如您所知,旧式权利已贬值

于 2013-12-31T03:11:37.307 回答