Results 1 to 6 of 6

Thread: [RESOLVED] how to get a value from part of a url ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Resolved [RESOLVED] how to get a value from part of a url ?

    Hi i got few urls:
    example:

    Code:
    http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt
    in a listbox i want to extract part of the url for example this part:1234567891 from the url when clicking on any url in listbox and place 1234567891 in a textbox for further process.could any one show me how this can be done.Thanks

    url example:
    Code:
    http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt
    Last edited by tony007; Mar 24th, 2011 at 10:03 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to get a value from part of a url ?

    vb.net Code:
    1. Dim urlAddress As String = "http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt"
    2. TextBox1.Text = Split(urlAddress, "/").FirstOrDefault(Function(x) IsNumeric(x) = True)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: how to get a value from part of a url ?

    Quote Originally Posted by Pradeep1210 View Post
    vb.net Code:
    1. Dim urlAddress As String = "http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt"
    2. TextBox1.Text = Split(urlAddress, "/").FirstOrDefault(Function(x) IsNumeric(x) = True)
    That is VB.Net code though, not VB6 (which is the forum)!

    The idea is the same, you just need to do it slightly differently:

    First step is to Split so you get an array with all bits of the URL

    After that, if you always want the 4th/5th/n-th part of the URL, you can just get it directly from the array.

    If you want the part of the URL which is always numeric, then loop through each element of the array checking if the value is numeric or not (that's what the VB.net code does above, just using newer functions).


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: how to get a value from part of a url ?

    i think i fixed it by doing first removing the /DisplayDetails.wmt then got the last part of url:

    Code:
    Dim withParts As String
    Dim withoutParts As String
    
    withParts = "http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt"
    
    withoutParts = Replace(withParts, "/DisplayDetails.wmt", "")
    
    MsgBox "Text after last / is " & Mid$(withoutParts, InStrRev(withoutParts, "/") + 1)
    Last edited by tony007; Mar 24th, 2011 at 11:55 AM.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: how to get a value from part of a url ?

    I don't know if i got you well, but tell me does the following help you ?

    VB Code:
    1. Dim string1 As String
    2.  Dim string2 As String
    3.  Dim Result As String
    4.  Dim Position As Integer
    5.  
    6.  string1 = "http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt"
    7.  string2 = "/"
    8.  
    9. 'To get the position of the \ starting from right
    10. Position = InStrRev(string1, string2, Len(string1))
    11.  
    12. 'Result after removing the part of the string that comes after the last /
    13. Result = Mid(string1, 1, Position)
    14.  
    15. ' Result will be : "http://www.somesite.com/m2/side/1234567891/"

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: how to get a value from part of a url ?

    Quote Originally Posted by manavo11 View Post
    That is VB.Net code though, not VB6 (which is the forum)!

    The idea is the same, you just need to do it slightly differently:

    First step is to Split so you get an array with all bits of the URL

    After that, if you always want the 4th/5th/n-th part of the URL, you can just get it directly from the array.

    If you want the part of the URL which is always numeric, then loop through each element of the array checking if the value is numeric or not (that's what the VB.net code does above, just using newer functions).
    Sorry.. I posted in the wrong forum.

    This is the VB6 equivalent:
    vb Code:
    1. Dim urlAddress As String
    2. urlAddress = "http://www.somesite.com/m2/side/1234567891/DisplayDetails.wmt"
    3.  
    4. Dim parts() as String, part as String
    5. parts = Split(urlAddress, "/")
    6. For each part in parts
    7.     If IsNumeric(part) Then
    8.         TextBox1.Text = part
    9.         Exit For
    10.     End If
    11. Next
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

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



Click Here to Expand Forum to Full Width