2016-03-04 141 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 
相關問題