1. if Length(What) > Length(InWhat) then Exit; unnecessarily.
Fast way out.
4. Incorrect result if the part of the string matches the already found fragment (see example below). In this case, you need to skip the entire "What".
That's a matter of definition.
If InWat = 'aaaa'
and What = 'aa'
I would say that 'aa' occurrs 3 times an 'aaaa':
aaaa, a
aaa and aa
aa.
If you don't agree with this, use a while loop instead of for and increment i with length(what) if NoCigar=False.
This could be controlled by a parameter of course.
Also casesensistivity can be implemented and controlled by a parameter.
I leave that as an excercise to topic starter.
As a final remark: none of the code posted above (including my own) is enterprisy enough.
This needs a dedicated factory with abstract classes, configurable via
XML (make that JSON, it's more of a hype now than XML) and it needs to support all stringtypes and character encoding (including Morse, EBCDIC etc.) anyone can imagine.
I challenge Thaddy to implement it that way
Bart