我目前正在嘗試製作一個應用程序,它將替換兩個字符串之間的值,但我使用的代碼不起作用,任何人都知道如何正確執行此操作?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
這是我得到的方式錯誤「類型‘System.ArgumentException’未處理的異常發生在Microsoft.VisualBasic.dll中 其他信息:參數‘長度’必須大於或等於零「。 –
我假設這個錯誤是? nIndexEnd - nIndexStart - sDelimStart.Length? 爲什麼你不能使用替換功能? – Pure
是的,因爲我想要替換的值總是不同,但前後的字符串總是相同的,所以我需要找到值並替換 –