1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to trim string(s)/URL(s) to last folder?

Discussion in 'Visual Basic .NET' started by flann, Aug 5, 2012.

  1. flann

    flann Regular Member

    Joined:
    Jan 19, 2008
    Messages:
    208
    Likes Received:
    34
    I have this (in a RichTextBox):
    Code:
    http://www.site.com/folder1/folder2/
    http://www.site.com/folder1/folder2/234/
    http://www.site.com/folder2/folder2/sagdg
    http://www.site.com/folder2/folder4/fdgdf
    
    I need this (i need to remove all crap after 1st directory/folder):
    Code:
    http://www.site.com/folder1/
    http://www.site.com/folder2/
    
    Or maybe someone can tell me how to extract all links on a page with specific classname?
    example: <a href="ExtractThisCrap" title="Random Crap" class="ExtractMe"> (i need the ExtractThisCrap ;) i don't get it, maybe it is not working for me because the class is behind the crap/URL i need)
     
    Last edited: Aug 5, 2012
  2. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    find the position of the //

    using that as your start, find the position of the first / or the end of the string if there isn't one.

    then take that and find either the next / or the end of the string. take everything from there forward.

    use the .IndexOf() method off the string class as well as the .Split() method.
     
  3. jj89XD

    jj89XD Newbie

    Joined:
    Sep 4, 2012
    Messages:
    3
    Likes Received:
    1
    You could use regex.

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For Each xlink In RichTextBox1.Lines                 ' Replace xx with tt
                Dim MC As MatchCollection = Regex.Matches(xlink, "hxxp://(.*?)/(.*?)/")
                For Each m As Match In MC
                    Dim g1 As Group = m.Groups(1)
                    Dim g2 As Group = m.Groups(2)
                                        ' Replace xx with tt
                    ListBox1.Items.Add("hxxp://" & g1.ToString & "/" & g2.ToString & "/")
                Next
            Next
    
        End Sub
    
    End Class