2 functions from MSSL, that you might find useful:
PosAll
Simba Code:
function PosAll(s, str: string): TIntegerArray;
var
sL, strL, o, p, r: Integer;
begin
sL := Length(s);
strL := Length(str);
if sL > strL then
Exit;
SetLength(Result, strL);
repeat
p := PosEx(s, str, (o + 1));
if p > 0 then
begin
Result[r] := p;
o := p;
Inc(r);
end;
until p <= 0;
SetLength(Result, r);
end;
PosAll SHORTER edition
Simba Code:
function PosAll(s, str: string): TIntegerArray;
var
strL, p, r: Integer;
begin
strL := Length(str);
if Length(s) > strL then
Exit;
SetLength(Result, strL);
repeat
p := PosEx(s, str, (p + 1));
if p > 0 then
Result[r] := p;
Inc(r);
until p <= 0;
SetLength(Result, (r - 1));
end;
Count
Simba Code:
function Count(s, str: string): Integer;
var
p: Integer;
begin
if Length(s) <= Length(str) then
repeat
p := PosEx(s, str, (p + 1));
if p > 0 then
Inc(Result);
until p <= 0;
end;
Count2 (Method 2... Seems to work faster than Method 1.)
Simba Code:
function Count2(s, str: string): Integer;
var
i, sL, strL: Integer;
begin
sL:= Length(s);
strL := Length(str);
if sL > strL then
Exit;
for i := 1 to ((strL - sL) + 1) do
if Copy(str, i, sL) = s then
Inc(Result);
end;