Solved

Parse text out of URL

Posted on 2008-10-16
3
316 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now