CountStr

From SCAR Divi Manual
Revision as of 12:01, 22 May 2012 by Freddy (talk | contribs) (Definition)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Definition

function CountStr(const SubStr, Str: string): Integer;

Source Code

function CountStr(const SubStr, Str: string): Integer;
var
  StrPos, StrLen: Integer;
begin
  Result := 0;
  StrLen := Length(SubStr);
  if StrLen = 0 then Exit;
  StrPos := 1;
  repeat
    StrPos := PosEx(SubStr, Str, StrPos);
    if StrPos > 0 then
    begin
      Inc(Result);
      Inc(StrPos, StrLen);
    end else Break;
  until False;
end;

Availability

SCAR Divi 3.34 > Current

Description

Counts every instance of a string SubStr in a string Str. If SubStr is empty, the function returns 0.

Example

begin
  WriteLn(CountStr('lol', 'lollol  lol heylol'));
end.

Output:

4