Strip string before # (a charc)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code937
    New Member
    • Sep 2007
    • 24

    Strip string before # (a charc)

    Hey Guys,

    First time here, i usually resort to google (but for the first time in 3 years he (or she) cant help me) ... Im making some software that generates html reports which can be viewed within the program and they're exported with bookmark links so that when you click a link (bookmark) it adds the bookmark to the end of the address (changes the window to http://address/file.html#timmy) as this happens the program realises the address has changed so it grabs the new address and sticks it into a string ... i then want to strip that string anything before and including the #.

    so in this case i want to be left with the word timmy
    (in actual terms, this well be a membership number not a name but it shouldnt make any difference )

    i have this, but it keeps returning nothing. Perhaps it'll give you something to work with ...

    Code:
    Public Function DeleteString( _
          ByVal vString As Variant _
        , Optional ByVal vStartPos As Variant _
        , Optional ByVal vLength As Variant _
        ) As Variant
        
        End Function
    
    Public Sub addresschange()
    Dim address As String
    Dim membername As String
    
    address = web.LocationURL
    findbm = "#"
    thename = InStr(address, findbm)
    membername = DeleteString(address, 1, thename)
    
    MsgBox membername 'membername is null ??
    
    end sub
    ignore how the program recognises how the routine is called, i simply want to strip the string ... could someone put me out my misery please :)
  • cugone
    New Member
    • Sep 2007
    • 20

    #2
    Because you did not mention what version of Visual Basic you are writing in, I'm going to assume 2005. :)

    Anyway, it's a very simple process to extract a string using the String.substrin g and String.IndexOf methods:

    Code:
            Dim address As String = "http://address/file.html#timmy"
            Dim strMemberName As String
            strMemberName = address.Substring(address.IndexOf("#") + 1)
    The substring method parameters are startIndex and length. If length is omitted the substring returned is from startIndex to the end of the string, including startIndex.

    The IndexOf method returns the index of a specific character in a string. Because there can only be one octothorpe in any given URI you get the index of that specific character. Since you do not want the octothorpe included you add one to the returned index.

    Hope that helps.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by code937
      i have this, but it keeps returning nothing. Perhaps it'll give you something to work with ...
      Looks to me as though it's returning nothing because the DeleteString routine does nothing. Perhaps you left something out, there?

      By the way, cugone, though I could be mistaken, I think the use of Variant data type probably indicates VB6. Wasn't that replaced by Object in VB.Net?

      Comment

      • cugone
        New Member
        • Sep 2007
        • 20

        #4
        Originally posted by Killer42
        Looks to me as though it's returning nothing because the DeleteString routine does nothing. Perhaps you left something out, there?

        By the way, cugone, though I could be mistaken, I think the use of Variant data type probably indicates VB6. Wasn't that replaced by Object in VB.Net?
        Why yes, yes it was I didn't notice it because he said not to pay attention to that routine. :/ Well, code937, if you ever switch to 2005, that's how you do it.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by cugone
          Why yes, yes it was I didn't notice it because he said not to pay attention to that routine. :/
          First law of debugging, I think - don't believe what anyone tells you, check it out for yourself. :)

          Comment

          • code937
            New Member
            • Sep 2007
            • 24

            #6
            Hey guys, I almost forgot I posted this until I went back to this part of the program and realised I still hadn't worked out how to do this. Yeah I'm using VB6 still ... I know I should update (of which I have got a copy of .Net) but I can do so much in VB6 that I wouldn't have a clue about in .Net .... anywho, I'm gunna check out that bit of code in VB6.

            Cheers

            --- update, no because I'm using VB6 that code doesnt work :$ --

            Any chance of a lickle teeny bit more guidance?

            Comment

            • code937
              New Member
              • Sep 2007
              • 24

              #7
              i hate to bump the thread but can anyone else help ? :)

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by code937
                i hate to bump the thread but can anyone else help ? :)
                :O How rude! ;)

                Let's see, if I remember correctly (after a quick flick back through the thread), you want a VB6 routine to take something like "http://address/file.html#timmy " and return the part after the "#". Seems simple enough.

                [CODE=vb]Public Function Version1(ByVal OriginalString As String, ByVal Delimiter As String) As String
                Version1 = Split(OriginalS tring, Delimiter)(1)
                End Function

                Public Function Version2(ByVal OriginalString As String, ByVal Delimiter As String) As String
                Version2 = Mid$(OriginalSt ring, InStr(1, OriginalString, Delimiter) + 1)
                End Function[/CODE]There's two different functions that do much the same thing - take you pick.

                Note that this is more or less "proof of concept" code. It doesn't take into account situations such as empty strings, no "#" in the string, and so on.

                Comment

                Working...