获取2个字符串之间的所有字符

时间:2013-05-06 08:50:19

标签: vb.net

   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个字符串之间的第一个字符串帮助对不起很难解释:/

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

答案 2 :(得分: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

旧样式权利按您所知折旧