2015-03-02 39 views
0

我需要拉數據,然後輸出兩行。輸入爲STREET/CITY/STATE/ZIP。我的下面的代碼適用於街道和城市,在STATE它發送ST/ZIP輸出。我似乎可以找到我的錯誤?代碼如下:VB insrt需要從字符串中拉文本

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

我建議使用[String.Split方法](https://msdn.microsoft.com/en-us/library/system.string.split(V = vs.110)的.aspx) – bansi 2015-03-02 04:59:01

回答

0

您可以使用String.Split在.net中專門針對這些問題嘗試下面的代碼。

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 
+0

由於但是,對於這個建議,錯誤指出「split」不是System.Windows.Forms.textbox的成員。 – 2015-03-02 06:41:49

+0

Oups,抱歉錯字錯誤,我錯過了'.text'屬性。我更新了答案,你可以請重試嗎? – 2015-03-02 07:59:56

+0

謝謝,那就是它 – 2015-03-03 05:49:33

相關問題