Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parse text out of URL

Posted on 2008-10-16
3
Medium Priority
?
340 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

886 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