在 Visual Basic 中复制两个分隔符之间的固定内容
copy fixed content between two delimeter in visual basic
我想将两个定界符号之间的固定词从一个文本框复制或转移到 another.i 成功地将单个词从一个文本框转移到另一个文本框,但是当我想转移两个或更多词时,它显示了 error.the 按下按钮时发生了单词的传输。
我在分隔符号之间传输单个单词的代码是:-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimEnd As String = "." '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
TextBox2.Text = res 'Display
End If
End Sub
上面的代码工作正常,但是当我想搜索和传输两个词时,它显示错误为 ArgumentException was Unhandled and Argument 'Length'必须大于或等于零 。我的错误包含代码是:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sSource1 As String = TextBox1.Text
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimStart1 As String = "LastName="
Dim sDelimEnd As String = "." 'Second delimiting word
Dim sDelimEnd1 As String = "."
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.IndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.IndexOf(sDelimEnd1)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
If nIndexStart1 > -1 AndAlso nIndexEnd1 > -1 Then '-1 means the word was not found.
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1 + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length) 'Crop the text between
TextBox3.Text = res1 'Display
End If
End Sub
我认为错误是由于在 'if' 语句中重复使用了 Length 变量,但我不知道如何将 that.The link 修复为我想要的快照输出是:http://tinypic.com/r/1zy7iwz/8 我只能转移单词 harry 但不能转移 porter.Any 非常感谢帮助。
首先,我认为您需要在 sDelimStart
和 sDelimStart1
之后添加 .length
。目前,您正在尝试将字符串 + 整数作为整数传递。
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)
在你的第二个 if 语句中:
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1.length + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length)
不过,我建议使用 String.Split
函数:
https://msdn.microsoft.com/en-us/library/b873y76a%28v=vs.110%29.aspx
编辑:为什么在两者中显示相同的内容。
我认为这是因为您需要找到分隔符的最后一个索引。
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.LastIndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.LastIndexOf(sDelimEnd1)
我想将两个定界符号之间的固定词从一个文本框复制或转移到 another.i 成功地将单个词从一个文本框转移到另一个文本框,但是当我想转移两个或更多词时,它显示了 error.the 按下按钮时发生了单词的传输。 我在分隔符号之间传输单个单词的代码是:-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimEnd As String = "." '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
TextBox2.Text = res 'Display
End If
End Sub
上面的代码工作正常,但是当我想搜索和传输两个词时,它显示错误为 ArgumentException was Unhandled and Argument 'Length'必须大于或等于零 。我的错误包含代码是:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sSource As String = TextBox1.Text 'String that is being searched
Dim sSource1 As String = TextBox1.Text
Dim sDelimStart As String = "FirstName=" 'First delimiting word
Dim sDelimStart1 As String = "LastName="
Dim sDelimEnd As String = "." 'Second delimiting word
Dim sDelimEnd1 As String = "."
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.IndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.IndexOf(sDelimEnd1)
If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
TextBox2.Text = res 'Display
End If
If nIndexStart1 > -1 AndAlso nIndexEnd1 > -1 Then '-1 means the word was not found.
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1 + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length) 'Crop the text between
TextBox3.Text = res1 'Display
End If
End Sub
我认为错误是由于在 'if' 语句中重复使用了 Length 变量,但我不知道如何将 that.The link 修复为我想要的快照输出是:http://tinypic.com/r/1zy7iwz/8 我只能转移单词 harry 但不能转移 porter.Any 非常感谢帮助。
首先,我认为您需要在 sDelimStart
和 sDelimStart1
之后添加 .length
。目前,您正在尝试将字符串 + 整数作为整数传递。
Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.length + 1, nIndexEnd - nIndexStart - sDelimStart.Length)
在你的第二个 if 语句中:
Dim res1 As String = Strings.Mid(sSource1, nIndexStart1 + sDelimStart1.length + 1, nIndexEnd1 - nIndexStart1 - sDelimStart1.Length)
不过,我建议使用 String.Split
函数:
https://msdn.microsoft.com/en-us/library/b873y76a%28v=vs.110%29.aspx
编辑:为什么在两者中显示相同的内容。
我认为这是因为您需要找到分隔符的最后一个索引。
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexStart1 As Integer = sSource1.LastIndexOf(sDelimStart1)
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2
Dim nIndexEnd1 As Integer = sSource1.LastIndexOf(sDelimEnd1)