News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

FindChr - Find the next occurrence of a character within a string

Started by kevin, March 16, 2009, 01:30:00 AM

Previous topic - Next topic

kevin

  FindChr

 This function find the next occurrence of a character within a string.  It's basically a custom version instring where you search for group of individual characters, rather than a  word.


PlayBASIC Code: [Select]
   SearchString$="Hello       World"

repeat
WhiteSpacePosition=FindChr(SearchString$," "+chr$(9),WhiteSpacePosition+1)
print WhiteSpacePosition
until WhiteSpacePosition=0

Sync
waitkey




Psub FindChr(SearchString$,ChrList$,StartPos)
if StartPos<1 Then StartPos=1
ThisPosition=0
TempChrlist$=ThisChr$
For pos=StartPos to len(SearchString$)
ThisChr=mid(SearchString$,pos)
for lp=1 to len(ChrList$)
if ThisChr=mid(Chrlist$,lp)
ThisPosition=pos
exitFor pos
endif
next
next
EndPsub ThisPosition






geecee

Hi kevin

Slight adaptation and it can be used to determine the number of times a letter appears in a string

SearchString$="Hello World"
   
` Letter to check for appearances
letter$="l"

  repeat
    WhiteSpacePosition=FindChr(SearchString$,letter$+chr$(9),WhiteSpacePosition+1)
      if whitespaceposition
        inc k
      endif
  until WhiteSpacePosition=0
   
print ""
print "The letter "+letter$+" appears "+str$(k)+" times in "+SearchString$

Sync
waitkey


;)
Cheers
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

kevin

 For that, you'd be better off using 'InString'


PlayBASIC Code: [Select]
searchString$="Hello World"

` Letter to check for appearances
letter$="l"

repeat
WhiteSpacePosition=Instring(SearchString$,letter$,WhiteSpacePosition+1,false)
if whitespaceposition
inc k
endif
until WhiteSpacePosition=0

print ""
print "The letter "+letter$+" appears "+str$(k)+" times in "+SearchString$

Sync
waitkey





geecee

Thanks for your reply kevin.

Something else I have learned.

:)
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)