Difference between revisions of "CountStr"

From SCAR Divi Manual
Jump to: navigation, search
(Description)
(Definition)
 
Line 10: Line 10:
 
   StrPos, StrLen: Integer;
 
   StrPos, StrLen: Integer;
 
begin
 
begin
 +
  Result := 0;
 
   StrLen := Length(SubStr);
 
   StrLen := Length(SubStr);
   if StrLen = 0 then
+
   if StrLen = 0 then Exit;
    Exit(0);
+
  StrPos := 1;
   StrPos := Pos(SubStr, Str);
+
   repeat
  if StrPos > 0 then
+
    StrPos := PosEx(SubStr, Str, StrPos);
  begin
+
    if StrPos > 0 then
    Result := 1;
 
    while True do
 
 
     begin
 
     begin
       StrPos := PosEx(SubStr, Str, StrPos + StrLen);
+
       Inc(Result);
       if StrPos > 0 then
+
       Inc(StrPos, StrLen);
        Inc(Result)
+
     end else Break;
      else
+
   until False;
        Break;
 
     end;
 
   end else Result := 0;
 
 
end;
 
end;
 
</source>
 
</source>

Latest revision as of 11:01, 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
  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