Difference between revisions of "CountStr"
From SCAR Divi Manual
(→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; |
− | + | StrPos := 1; | |
− | StrPos := | + | repeat |
− | + | StrPos := PosEx(SubStr, Str, StrPos); | |
− | + | if StrPos > 0 then | |
− | |||
− | |||
begin | begin | ||
− | + | Inc(Result); | |
− | + | Inc(StrPos, StrLen); | |
− | + | end else Break; | |
− | + | until False; | |
− | |||
− | end; | ||
− | |||
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