CountStr

From SCAR Divi Manual
Revision as of 11:46, 22 May 2012 by Freddy (talk | contribs) (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...")
(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
  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