Page 1 of 1

FindFirstOf & FindFirstNotOf string searching functions

Posted: Fri Mar 05, 2010 1:14 pm
by milan1612
Two small functions I needed recently. See the bottom of the post for a brief description.

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
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.

Re: FindFirstOf & FindFirstNotOf string searching functions

Posted: Sat Mar 06, 2010 2:53 am
by IdeasVacuum
Hello milan1612

Both work very nicely, thank you for sharing.