Reputation: 11
How to split the string "V237P023F50.5" into different text boxes.
I am trying this code:
Dim input As String = "V237P023F50.5"
TextBox1.Text = input
Dim parts As String() = input.Split(New String() {"V"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts1 As String() = input.Split(New String() {"V", "P"}, StringSplitOptions.RemoveEmptyEntries)
Dim parts2 As String() = input.Split(New String() {"V", "P", "F"}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To parts.Length - 1
If i > 0 OrElse input.StartsWith("V") = True Then
parts(i) = "" & parts(i)
TextBox2.Text = parts(i)
End If
Next
For i As Integer = 0 To parts1.Length - 1
If i > 0 OrElse input.StartsWith("P") = True Then
parts1(i) = "" & parts1(i)
TextBox4.Text = parts1(i)
End If
Next
For i As Integer = 0 To parts2.Length - 1
If i > 0 OrElse input.StartsWith("F") = True Then
parts2(i) = "" & parts2(i)
TextBox5.Text = parts2(i)
End If
Next
Expected output
V237
P023
F50.5
Please help me.
Upvotes: 0
Views: 368
Reputation: 1234
If this is fixed length then use Substring().
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(0, 4)
TextBox4.Text = oldstring.Substring(4, 4)
TextBox5.Text = oldstring.Substring(8)
If the alphacharacters are the same then you can use IndexOf() with Substring.
Dim oldstring As String = "V237P023F50.5"
TextBox2.Text = oldstring.Substring(oldstring.IndexOf("V" ), (oldstring.IndexOf("P") - oldstring.IndexOf("V")))
TextBox4.Text = oldstring.Substring(oldstring.IndexOf("P" ), (oldstring.IndexOf("F") - oldstring.IndexOf("P")))
TextBox5.Text = oldstring.Substring(oldstring.IndexOf("F" ))
Upvotes: 1