PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
string manipulation-VBForums
Results 1 to 13 of 13

Thread: string manipulation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    string manipulation

    Sorry, for the double posting; i previously posted as a reply

    I would like to trim a url to remove all characters after the third occurrence of a '/' if it exists.
    for example for the url:

    http://www.blahblahblah.hello/boo

    Remove the last '/' and boo.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Resolved Re: string manipulation

    This seems to work but if someone has a less clumsy way, please let me know.
    Code:
    Dim num As Integer  
    num = InStrRev(bloglink, "/")
              
    If num > 20 Then
       bloglink = Left(bloglink, num)
    End If

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: string manipulation

    Actually it does not if the Url has more than 4 occurences of '/'.

  4. #4
    Hyperactive Member BadgerBadger's Avatar
    Join Date
    Aug 2009
    Location
    Wales
    Posts
    382

    Re: string manipulation

    Loop through each character of the string and increment an integer every time the char is "/". If the integer equals 3 then get the index of that char, get the length of the whole string and then subtract it. Remove that many characters off the end.

    I'm sure there is a RegEx workaround to this, but I'm no master of it.

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: string manipulation

    Hey,

    Another alternative would be to Split the string on the / character:

    http://msdn.microsoft.com/en-gb/libr...it(VS.80).aspx

    The last element in the array will be "boo" in the example that you give. You should then find the instance of the this in the string, and then use Replace:

    http://msdn.microsoft.com/en-gb/libr...ce(VS.80).aspx

    I am not sure that a Regular Expression is needed in this case, but really it comes down to exactly what you are trying to do.

    Can you provide information as to what you are trying to achieve?

    Gary

  6. #6
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: string manipulation

    This should fix the problem of more than 3 occurences of "/".

    Code:
            Dim num As Integer = Strings.InStr(9, bloglink, "/", CompareMethod.Text)
            If num > 9 Then bloglink = Strings.Left(bloglink, num - 1)
    Alternatively you could start by searching for '//' and then use that as the starting position of your search for '/'.

    Code:
            Dim numa As Integer = Strings.InStr(bloglink, "//")
            Dim numb As Integer = Strings.InStr(numa + 2, bloglink, "/", CompareMethod.Text)
            If numb > 0 Then bloglink = Strings.Left(bloglink, numb - 1)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: string manipulation

    Basically I need to extract the main Url root only and nothing after the 4th "/".

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: string manipulation

    Hey,

    Probably the best way to do this would be to create a new Uri:

    http://msdn.microsoft.com/en-us/libr...m.uri.uri.aspx

    And from there, use one of the many properties of the Uri Class, such as Host:

    http://msdn.microsoft.com/en-us/libr....uri.host.aspx

    Hope that helps!!

    Gary

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: string manipulation

    Sorry guys, I tried all your ideas and no accurate results coming. Is there a regular expression for this or some string manipulation.

  10. #10
    Lively Member
    Join Date
    Sep 2009
    Posts
    99

    Re: string manipulation

    Let me try amplifying on a suggestion above that is a little awkward but should work.

    Here's what I hear you saying is the requirement: You want everything in front of the third slash in a URL string. So:

    Dim strParsed() As String = Nothing
    Dim strBuf As String = "test/with/many/gratuitous/slashes"
    Dim strAllDone As String = Nothing


    strParsed = strBuf.Split("/")

    strParsed now looks like: strParsed(0) = "test", strParsed(1) = with, ...

    Then reassemble:

    strAllDone = strParsed(0) & "/" & strParsed(1) & "/" strParsed(2) ' dunno if you need another slash

    You can use a stringbuilder if you have a lot of this to do.

  11. #11
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: string manipulation

    Try this:
    vb.net Code:
    1. Dim url As String = "http://www.blahblahblah.hello/boo/something/place.html"
    2. Dim replaced As String = Regex.Replace(url, "(?<=.+//.+)/.+", String.Empty)
    3. MessageBox.Show(replaced)

  12. #12
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: string manipulation

    Quote Originally Posted by cengineer View Post
    Sorry guys, I tried all your ideas and no accurate results coming. Is there a regular expression for this or some string manipulation.
    Hey,

    What exactly didn't work with my Uri suggestion?

    Gary

  13. #13
    Lively Member
    Join Date
    Sep 2009
    Posts
    99

    Re: string manipulation

    Here's another one if you're a "C" flavor of propeller hat...This implements the C strtok function in VB. It posts a couple warnings because not all branches return a value but it works pretty well. I've (ab)used it quite a bit as my thousands of years of programming experience are primarily c/c++.

    http://www.freevbcode.com/ShowCode.asp?ID=4310

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width