Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- Explanation: Counts all strings (s) in text (str).
- If overlap is set to true, strings can overlap.
- ('test', 'testestest', False) => 2
- ('test', 'testestest', True) => 3
- [==============================================================================}
- function CountStrEx(s, str: string; overlap: Boolean): Integer;
- var
- p, o: Integer;
- begin
- Result := 0;
- if not overlap then
- begin
- o := Length(s);
- p := (p - (o - 1));
- end else
- o := 1;
- repeat
- p := PosEx(s, str, (p + o));
- if (p > 0) then
- Inc(Result);
- until (p <= 0);
- end;
- var
- s, str: string;
- begin
- ClearDebug;
- str := 'TESTESTESTEST for CountStrEx()!';
- s := 'TEST';
- WriteLn('String: "' + str + '"');
- WriteLn('Count of "' + s + '" in String: ' + IntToStr(CountStrEx(s, str, True)) + ' (overlap = ON)!');
- WriteLn('Count of "' + s + '" in String: ' + IntToStr(CountStrEx(s, str, False)) + ' (overlap = OFF)!');
- end.
Advertisement
Add Comment
Please, Sign In to add comment