CountStr

From SCAR Divi Manual
Revision as of 10:47, 22 May 2012 by Freddy (talk | contribs) (Description)
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
  StrLen := Length(SubStr);
  if StrLen = 0 then
    Exit(0);
  StrPos := Pos(SubStr, Str);
  if StrPos > 0 then
  begin
    Result := 1;
    while True do
    begin
      StrPos := PosEx(SubStr, Str, StrPos + StrLen);
      if StrPos > 0 then
        Inc(Result)
      else
        Break;
    end;
  end else Result := 0;
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