FindFirstOf & FindFirstNotOf string searching functions
Posted: Fri Mar 05, 2010 1:14 pm
Two small functions I needed recently. See the bottom of the post for a brief description.
FindFirstOf searches the string (first parameter) for any characters contained in the second string
parameter and returns the position of the first occurence of one. Returns 0 if none was found.
FindFirstNotOf does almost the same but instead returns the position of the first character in the
first string which is not contained in the second one. It too returns 0 if none was found.
Code: Select all
Procedure FindFirstOf(str.s, chars.s)
Protected *chr.Character = @str
Protected pos = 1
Protected *chr2.Character
While *chr\c
*chr2 = @chars
While *chr2\c
If *chr\c = *chr2\c
ProcedureReturn pos
EndIf
*chr2 + SizeOf(Character)
Wend
*chr + SizeOf(Character)
pos + 1
Wend
EndProcedure
Procedure FindFirstNotOf(str.s, chars.s)
Protected *chr.Character = @str
Protected pos = 1
Protected *chr2.Character
Protected gotit
While *chr\c
*chr2 = @chars
gotit = 0
While *chr2\c
If *chr\c = *chr2\c
gotit = 1
Break
EndIf
*chr2 + SizeOf(Character)
Wend
If Not gotit
ProcedureReturn pos
EndIf
*chr + SizeOf(Character)
pos + 1
Wend
EndProcedure
Debug FindFirstOf("this is a test string!", "ice") ; 3
Debug FindFirstNotOf("this is a test string!", "ice") ; 1
parameter and returns the position of the first occurence of one. Returns 0 if none was found.
FindFirstNotOf does almost the same but instead returns the position of the first character in the
first string which is not contained in the second one. It too returns 0 if none was found.