Solved

Parse text out of URL

Posted on 2008-10-16
3
320 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:zerogeek
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 22729107
Hello zerogeek,

txtName.Text = txtLink.Text.Substring(txtLink.Text.LastIndexOf("/")+1)

Should do it.

Regards,

TimCottee
0
 

Author Comment

by:zerogeek
ID: 22729153
Worked Perfect! Thanks!
0
 
LVL 2

Expert Comment

by:JasonChandler
ID: 22729177
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

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question