Extract words from strings?

Blitz3D Forums/Blitz3D Programming/Extract words from strings?

Yue(Posted 2016) [#1]
patch$: = "C:\Maps\LevelMap.map"


How can I extract the last word LevelMap.map, so that it is stored in a variable?

print map$ ; Text on screen LevelMap.map



Midimaster(Posted 2016) [#2]
Search for the last Backslash and use this position to cut the string:
Print Filename("C:\Maps\LevelMap.map")

Function FileName$(Path$)
	Local Where%=LastInstr(Path,"\")
	Return Mid(Path,Where,999)
End Function


Function LastInstr%(Txt$, Search$)
	Local Where%, Here%
	Repeat
		Where=Here+1
		Here= Instr(Txt, Search, Where)
	Until Here=0	
	Return Where
End Function



Yue(Posted 2016) [#3]
@Midimaster
Thanks You. :)


Bobysait(Posted 2016) [#4]
You'd better reverse the string instead of using the InStr function.

; returns "0" if not found
Function FindLast%(s$, find$)
    For i = Len(s) To 1 Step -1
        If Mid(s, i,1)=find Then Return i
    Next
    Return 0
End Function

Local path$ = "C:\Maps\LevelMap.map"
Print Right( path, Len(path) - FindLast(path, "\" ) )


(untested, but it should work I suppose)