Link to home
Start Free TrialLog in
Avatar of zerogeek
zerogeekFlag for United States of America

asked on

Parse text out of URL

I need to be able to grab a part of a URL and place in a textbox.
I want to grab the last part of the URL from a textbox, and place in another textbox. I need everything from the end of the url, to the last /.
Example:
textbox1.text = "http://www.site.com/stuff/stuffz/thing.exe
textbox2.text = "thing.exe"

I have tried using the function and code below, but it doesn't work the way I need it.

It just returns the URL without the / 's.

Any help is appreciated.
Public Function midReturn(ByVal first As String, ByVal last As String, ByVal total As String) As String
        If last.Length < 1 Then
            midReturn = total.Substring(total.IndexOf(first))
        End If
        If first.Length < 1 Then
            midReturn = total.Substring(0, (total.IndexOf(last)))
        End If
        Try
            midReturn = ((total.Substring(total.IndexOf(first), (total.IndexOf(last) - total.IndexOf(first)))).Replace(first, "")).Replace(last, "")
        Catch ArgumentOutOfRangeException As Exception
        End Try
    End Function
 
    Private Sub btnClip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClip.Click
 
        txtName.Text = midReturn("/", "", txtLink.Text)
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zerogeek

ASKER

Worked Perfect! Thanks!
Avatar of JasonChandler
JasonChandler

This should do what you need.
Set up a page with to textboxes on it and please test.
I have set this up in the Page_Load Event however you put this in a function.

Hope this helps
Jason
Code behind
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim s As String = "http://www.site.com/stuff/stuffz/thing.exe"
        Dim lengthOfFullString As Integer = s.Length
        Dim i As Integer = s.LastIndexOf("/")
        Dim texbox1Text As String = s.Remove(i) ' If you want the / at the end then just add + "/"
        Dim texbox2Text As String = s.Substring(i)
        Me.TextBox1.Text = texbox1Text
        Me.TextBox2.Text = texbox2Text
    End Sub
 
ASPX PAGE
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    </div>

Open in new window