[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!
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
Re: Remove text from String
try this
vb Code:
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
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
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:thumb:
Re: [RESOLVED] Remove text from String
So does TBeck's and if you change Text1.Text in his to MyString then his is arguably a better (more compact) solution.
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! :D