2016-03-04 140 views
0

我目前正在尝试制作一个应用程序,它将替换两个字符串之间的值,但我使用的代码不起作用,任何人都知道如何正确执行此操作?vb.net查找两个字符串之间的字符串

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 
+0

这是我得到的方式错误“类型‘System.ArgumentException’未处理的异常发生在Microsoft.VisualBasic.dll中 其他信息:参数‘长度’必须大于或等于零“。 –

+0

我假设这个错误是? nIndexEnd - nIndexStart - sDelimStart.Length? 为什么你不能使用替换功能? – Pure

+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 
+0

这工作谢谢百万 –

+0

对不起,没有解释,但我想通过查找“Substring”函数很容易计算出这条线的工作原理。我拿出你的其他代码,因为我认为这是不必要的。 – Pure

+0

我把它拿回来这最终不工作它只有在我使用上面的确切代码时才起作用,但如果我尝试做一些类似的源字符串是从richtextbox那有一个巨大的字符串与该部分在其中但不是整个字符串,然后它搞砸 –

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) 

String你知道的。 startIndex只是lenght第一部分加一个这样sDelimStart.Length + 1 lenght是

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

你有一对夫妇的问题(第一部分和最后部分的总和lenght)初始string减去lenght。请参见下面的代码:

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 
相关问题