vb.net 查找两个字符串之间的字符串
vb.net Find String between two strings
我目前正在尝试制作一个应用程序来替换两个字符串之间的值,但我使用的代码不起作用,有人知道如何正确执行此操作吗?
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
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
我帮你解决了:
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
长度是初始 string
减去(lenght
第一部分和最后一部分的总和)
sSource.Length - (sDelimStart.Length + sDelimEnd.Length)
你有几个问题。请看下面的代码:
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
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
我目前正在尝试制作一个应用程序来替换两个字符串之间的值,但我使用的代码不起作用,有人知道如何正确执行此操作吗?
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
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
我帮你解决了:
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
长度是初始 string
减去(lenght
第一部分和最后一部分的总和)
sSource.Length - (sDelimStart.Length + sDelimEnd.Length)
你有几个问题。请看下面的代码:
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
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