View Full Version : Spliting a String


harrisw
10-18-2001, 12:45 AM
Is there a way to find the first space from the right hand side of a string ie finding the surname out of a name string?

raskew
10-18-2001, 01:53 AM
This works like the Instr() function, in reverse.

Function xLastInStr(tstr As String, twhat As String) As Integer
'*******************************************
'Name: xLastInStr (Function)
'Purpose: Return location of last instance of a character or phrase.
'Inputs: Call xLastInStr("the quick brown fox jumped the lazy dog", "the")
'Output: 28 - Location of last occurence of "the"
'*******************************************

Dim i As Integer, n As Integer, tlen As Integer

n = 0
tlen = Len(twhat)
For i = Len(RTrim(tstr)) To 1 Step -1

If Mid(tstr, i, tlen) = twhat Then
n = i
Exit For
End If
Next i

xLastInStr = n

End Function