Difference between revisions of "CountStr"

From SCAR Divi Manual
Jump to: navigation, search
(Created page with "==Definition== <source lang="scar" lines="false"> function CountStr(const SubStr, Str: string): Integer; </source> ===Source Code=== <source lang="scar"> function CountStr(const...")
(No difference)

Revision as of 11:46, 22 May 2012

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.

Example

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

Output:

4