FindFirstOf & FindFirstNotOf string searching functions

Share your advanced PureBasic knowledge/code with the community.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

FindFirstOf & FindFirstNotOf string searching functions

Post 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.
Windows 7 & PureBasic 4.4
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: FindFirstOf & FindFirstNotOf string searching functions

Post by IdeasVacuum »

Hello milan1612

Both work very nicely, thank you for sharing.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply