Difference between revisions of "CountStr"
From SCAR Divi Manual
(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...") |
(→Definition) |
||
| (One intermediate revision by the same user not shown) | |||
| 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> | ||
| Line 33: | Line 29: | ||
==Description== | ==Description== | ||
| − | Counts every instance of a [[string]] '''SubStr''' in a string '''Str'''. | + | Counts every instance of a [[string]] '''SubStr''' in a string '''Str'''. If '''SubStr''' is empty, the function returns 0. |
==Example== | ==Example== | ||
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