Say I have the following strings:
[code]index.php/saint/elsewhere
and
/going/absolutely/nowhere[/code]
I need a function that will return:
elsewhere
nowhere
I need to be able to grab all the characters that occur after the right-most ‘/’ character. If there is no ‘/’ character then I need to return all the characters.
Any idea of the most efficient way to do this?
Hi Gary,
look up the functions split, nthfield and countfields.
HTH
Trixi
[code] Dim SearchString as String = “kjs// sdfkl .22w#$4/trailing”
Dim SearchCharacter As UInt8 = Asc("/")
Dim ByteLength As Int32 = SearchString.LenB()
Dim MemBlock As New MemoryBlock(ByteLength)
MemBlock.StringValue(0, SearchString.Len()) = SearchString
Dim CurrentCharacter As UInt8
Dim Position As Int32 = 0
For i As Integer = ByteLength - 1 DownTo 0
CurrentCharacter = MemBlock.UInt8Value(i)
If (CurrentCharacter = SearchCharacter) Then
Position = i + 2
Exit
End If
Next
Dim TrailingString As String = SearchString.Mid(Position)
MsgBox Str(TrailingString)[/code]
You’re lucky I felt like a quick challenge this morning…
EDIT: I’m pretty certain that this would fail, however, if your SearchString has characters with code values over 255 (or possibly, 127). Just something to be aware of, but I doubt this would impact what appears to be your intended usage.
trailing = url.NthField("/", url.CountFields("/"))
If you’re going to do this repeatedly, I’d see if a regular expression would be faster. One pattern would be:
[^/]+$
Thank you everybody.
[quote=17507:@Kem Tekinay]If you’re going to do this repeatedly, I’d see if a regular expression would be faster. One pattern would be:
[^/]+$[/quote]
Perfect.
This also works but is slightly slower than the RegEx.
One more thought:
dim parts() as string = url.Split( "/" )
dim trailing as string = parts( parts.Ubound )