Jack
Jack

Reputation: 119

Split String into Textboxes

I have this URL:

http://www.website.com/base.htm?age=<number>&Team=<number>&userID=<number>

How to get parts of this into Textboxes?

ie:

Textbox1 = number after ?age=
Textbox2 = number after &Team=
Textbox3 = number after &userID=

Upvotes: 0

Views: 113

Answers (2)

Bugs
Bugs

Reputation: 4489

If you want to output to a TextBox and can guarantee a set amount of parameters this is quite simple:

Dim url As String = "http://www.website.com/base.htm?age=15&Team=3&userID=1"

TextBox1.Text = url.Split("?"c)(1).Split("&"c)(0).Split("="c)(1)
TextBox2.Text = url.Split("?"c)(1).Split("&"c)(1).Split("="c)(1)
TextBox3.Text = url.Split("?"c)(1).Split("&"c)(2).Split("="c)(1)

The code looks a little unreadable but it does the job. Note the increase in number on the second Split. This is the output:

enter image description here

Now what I would do is further checking to ensure the parameters are there and that they have values:

Dim url As String = "http://www.website.com/base.htm?age=15&Team=3&userID=1"

Dim parameters As String = Nothing

If url.Contains("?") Then
    parameters = url.Split("?"c)(1)
End If

Dim age As Integer = 0
Dim team As Integer = 0
Dim userId As Integer = 0

If parameters IsNot Nothing Then

    For Each parameter In parameters.Split("&"c)

        If parameter.Contains("=") Then

            If parameter.ToLower().StartsWith("age") Then
                Integer.TryParse(parameter.Split("="c)(1), age)

            ElseIf parameter.ToLower().StartsWith("team") Then
                Integer.TryParse(parameter.Split("="c)(1), team)

            ElseIf parameter.ToLower().StartsWith("userid") Then
                Integer.TryParse(parameter.Split("="c)(1), userId)

            End If

        End If

    Next

End If

TextBox1.Text = age.ToString()
TextBox2.Text = team.ToString()
TextBox3.Text = userId.ToString()

The output is the same as above but I have done further checking. I'm sure even more checking could be put in place but I think this will give you a good start.

What I like to do is store both the name and the value of the parameter using a Dictionary which can come in handy so thought I would show you this approach:

Dim url As String = "http://www.website.com/base.htm?age=<number>&Team=<number>&userID=<number>"
Dim urlParameters As New Dictionary(Of String, String)

If url.Contains("?") AndAlso url.Contains("&") Then
   For Each param In url.Split("?"c)(1).Split("&"c)
        Dim kp() As String = param.Split("="c)
        urlParameters.Add(kp(0), kp(1))
    Next
End If

'ouput
For Each parameter In urlParameters
    Debug.WriteLine("Key: " & parameter.Key & " Value:" & parameter.Value)
Next

This is a screenshot of the output:

enter image description here

If you only want to look at the value of the parameter and output that then you could simply do this instead of adding to a Dictionary:

If url.Contains("?") AndAlso url.Contains("&") Then
    For Each param In url.Split("?"c)(1).Split("&"c)
        Debug.WriteLine(param.Split("="c)(1))
    Next
End If

In this case the output will be:

enter image description here

Upvotes: 2

F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

Dim url As String = "http://www.website.com/base.htm?age=15&Team=100&userID=1109"
Dim temp As String = url.Substring(url.IndexOf("?")+1)
Dim args() As String
args = temp.Split("&")
Dim pair() As String
For Each arg As String In args
    pair = arg.Split("=")
    console.WriteLine(pair(0))
    console.WriteLine(pair(1))  '   <---    value after =
Next

Upvotes: 2

Related Questions