Results 1 to 7 of 7

Thread: [RESOLVED] Remove text from String

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    110

    Resolved [RESOLVED] Remove text from String

    Hello,

    Does anyone know how to remove all the text after a certain point in a string?

    For example:

    MyString = "www.newfoundland.com/help/431234.html"

    How can I remove all the text after the last forward slash so it would return:

    sResult = "www.newfoundland.com/help/"

    Thanks in advance!

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Remove text from String

    I can think of several ways. This one may be too complicated but it works.

    Code:
        Dim MyString As String
        Dim lngIndex As Long
        Dim strParts() As String
        MyString = "www.newfoundland.com/help/431234.html"
        
        strParts = Split(MyString, "/")
        
        MyString = ""
        For lngIndex = 0 To UBound(strParts) - 1
            MyString = MyString & strParts(lngIndex) & "/"
        Next
        MyString = Left$(MyString, Len(MyString) - 1)
    
        MsgBox MyString

  3. #3
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Remove text from String

    try this

    vb Code:
    1. Text2.Text = Left(Text1.Text, InStrRev(Text1.Text, "/"))

    the InStrRev returns the position of the last occurance of the specified string. and left returns the first n characters of text1, and in this case n is the result of the InStrRev call

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Remove text from String

    Here's a better way.

    Code:
        Dim MyString As String
        Dim lngIndex As Long
        MyString = "www.newfoundland.com/help/431234.html"
        
        lngIndex = InStrRev(MyString, "/")
        
        MyString = Left$(MyString, lngIndex)
    
        MsgBox MyString

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    110

    Re: Remove text from String

    Quote Originally Posted by MartinLiss
    Here's a better way.

    Code:
        Dim MyString As String
        Dim lngIndex As Long
        MyString = "www.newfoundland.com/help/431234.html"
        
        lngIndex = InStrRev(MyString, "/")
        
        MyString = Left$(MyString, lngIndex - 1)
    
        MsgBox MyString
    Thanks! It works perfectly

  6. #6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    110

    Re: [RESOLVED] Remove text from String

    Quote Originally Posted by MartinLiss
    So does TBeck's and if you change Text1.Text in his to MyString then his is arguably a better (more compact) solution.
    I'm Sorry I didn't see TBeck's post.

    That works perfectly too!

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