Page 1 of 1
Dual purpose "CountString() "
Posted: Sat Feb 21, 2015 10:04 pm
by Randy Walker
Searched for similar thought but nothing quite like this turned up. We all know CountString() is great for returning occurrences. How useful would it be if it could also be used to return placement,similar to the FindString() function?
PROPOSED SYNTAX WOULD ADD OPTIONAL PARAMETER:
Result = CountString(String$, StringToCount$ [,Occurrence])
EXAMPLE (current design):
s$ = "word1,word2,word3,word4,word5"
Result = CountString(s$, "d")
Debug Result
5
EXAMPLE (new optional parameter):
s$ = "word1,word2,word3,word4,word5"
Result = CountString(s$, "d", 4)
Debug Result
22
GIves us 22 because that is the position of the 4th "d".
Re: Dual purpose "CountString() "
Posted: Tue Mar 10, 2015 4:10 pm
by kenmo
That would be handy, although I think it makes more sense as
a new function (such as FindOccurrence)
or a flag for FindString (since it returns a position)
instead of a flag for CountString (which should return a count, of course).
If anyone wants it, here is one simple implementation:
Code: Select all
Procedure.i FindOccurrence(String.s, StringToFind.s, Occurrence.i = 1, Mode.i = #PB_String_CaseSensitive)
If (String And StringToFind And Occurrence > 0)
Protected Start.i = 1
Protected Found.i = 0
Protected Pos.i
While (Found < Occurrence)
Pos = FindString(String, StringToFind, Start, Mode)
If (Pos)
Found + 1
If (Found = Occurrence)
ProcedureReturn (Pos)
Else
Start = Pos + Len(StringToFind)
EndIf
Else
Break
EndIf
Wend
EndIf
ProcedureReturn (0)
EndProcedure
Debug Str(CountString("word1,word2,word3,word4,word5", "d")) + " (count)"
Debug FindOccurrence("word1,word2,word3,word4,word5", "d")
Debug FindOccurrence("word1,word2,word3,word4,word5", "d", 4)
Debug FindOccurrence("word1,word2,word3,word4,word5", "z")
Debug FindOccurrence("aaaAAAaaa", "aaa", 2)
Debug FindOccurrence("aaaAAAaaa", "aaa", 2, #PB_String_NoCase)
Re: Dual purpose "CountString() "
Posted: Tue Mar 10, 2015 4:41 pm
by Little John
kenmo wrote:I think it makes more sense as
a new function (such as FindOccurrence)
or a flag for FindString (since it returns a position)
instead of a flag for CountString (which should return a count, of course).
Yep.
Re: Dual purpose "CountString() "
Posted: Wed Mar 11, 2015 10:07 pm
by davido
@
kenmo,
Thank you.
I'll find this useful.
