vb.net查找两个字符串之间的字符串

时间:2016-03-04 12:34:41

标签: vb.net string replace find

我目前正在尝试创建一个替换两个字符串之间的值的应用,但我使用的代码不起作用,任何人都知道如何正确地执行此操作?

Dim sSource As String = "64616D616765002D3100" 'String that is being searched
        Dim sDelimStart As String = "64616D61676500" 'First delimiting word
        Dim sDelimEnd As String = "00" 'Second delimiting word
        Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
        Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

        If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
            Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
            MessageBox.Show(res) 'Display
        Else
            MessageBox.Show("One or both of the delimiting words were not found!")
        End If

4 个答案:

答案 0 :(得分:0)

Dim sSource As String = "64616D616765002D3100" 'String that is being searched
Dim sDelimStart As String = "64616D61676500" 'First delimiting word
Dim sDelimEnd As String = "00" 'Second delimiting word

MsgBox(sSource.Substring(InStr(sSource, sDelimStart) + sDelimStart.Length - 1, sSource.Length - sDelimStart.Length - sDelimEnd.Length))

'MsgBox(sSource.Substring(InStr(sSource, sDelimStart) + sDelimStart.Length - 1, InStr(sSource.Substring(InStr(sSource, sDelimStart) + sDelimStart.Length - 1, sSource.Length - (InStr(sSource, sDelimStart) + sDelimStart.Length - 1)), sDelimEnd) 'use this line for longer or different text

答案 1 :(得分:0)

我为你修好了:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim sSource As String = "64616D616765002D3100" 'String that is being searched
    Dim sDelimStart As String = "64616D61676500" 'First delimiting word
    Dim sDelimEnd As String = "00" 'Second delimiting word
    Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
    Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

    If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.

        Dim res As String = Strings.Mid(sSource, sDelimStart.Length + 1, sSource.Length - (sDelimStart.Length + sDelimEnd.Length)) 'Crop the text between
        MessageBox.Show(res) 'Display
    Else
        MessageBox.Show("One or both of the delimiting words were not found!")
    End If
End Sub

让我们来看看这一行:

Dim res As String = Strings.Mid(sSource, sDelimStart.Length + 1, sSource.Length - (sDelimStart.Length + sDelimEnd.Length))

Strings.Mid(string to get middle part from, startIndex, lenght)
你得到了{p> StringstartIndex只是第一部分的lenght加上一部sDelimStart.Length + 1 长度是最初的string减去(lenght的第一部分和最后部分的总和)

sSource.Length - (sDelimStart.Length + sDelimEnd.Length)

答案 2 :(得分:0)

你有几个问题。请参阅以下代码:

    Dim sSource As String = "64616D616765002D3100" 'String that is being searched
    Dim sDelimStart As String = "64616D61676500" 'First delimiting word
    Dim sDelimEnd As String = "00" 'Second delimiting word
    Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
    Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd, nIndexStart + sDelimStart.Length + 1) 'Find the first occurrence of f2

    If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
        Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
        MessageBox.Show(res) 'Display
    Else
        MessageBox.Show("One or both of the delimiting words were not found!")
    End If

答案 3 :(得分:0)

Function extract(source As String, start As String, ending As String)
    Return source.Substring(InStr(source, start, CompareMethod.Text) + start.Length - 1, InStr(source, ending) - (InStr(source, start, CompareMethod.Text) + start.Length))
End Function