VB insrt需要从字符串中提取文本

时间:2015-03-02 04:54:48

标签: vb.net

我需要拉取数据,然后输出两行。输入以STREET / CITY / STATE / ZIP格式输入。我的代码适用于街道和城市,在STATE它将ST / ZIP发送到输出。我似乎找到了我的错误?这是代码:

Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
    Dim Street As String, City As String, State As String, Zip As String
    Dim npos As Integer = 0, npos2 As Integer, npos3 As Integer = 0
    npos = (InStr(txtAddress.Text, "/"))
    npos2 = (InStr((npos + 1), txtAddress.Text, "/"))
    npos3 = (InStr((npos2 + 1), txtAddress.Text, "/"))
    Street = (Mid(txtAddress.Text, 1, (npos - 1)))
    City = (Mid(txtAddress.Text, (npos + 1), (npos2 - npos - 1)))
    State = (Mid(txtAddress.Text, (npos2 + 1), (npos3 - 1)))
    TextBox1.Text = Street & vbCrLf & City & " " & State

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用专门针对.net中的这些问题实施的String.Split来尝试以下内容。

Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
    Dim Street As String, City As String, State As String, Zip As String
    Dim data() as string

    data = txtAddress.Text.Split("/"c)
    Street = data(0)
    City = data(1)
    State = data(2)
    Zip = data(3)

    TextBox1.Text = Street & vbCrLf & City & " " & State

End Sub