I want to count how many times a String occurs in another String in Pascal Script like shown in the below example.
I've seen the answer to Delphi: count number of times a string occurs in another string, but there is no PosEx
function in Pascal Script.
MyString := 'Hello World!, Hello World!, Hello World!, Hello World!';
If I count the number of times Hello
or World
occurs here, the result should be 4.
If I count the number of times ,
(comma) occurs here, the result should be 3.
UPDATE
The following function works, but it copies given String again to a new Variable, and deletes parts of Strings, so it works slowly.
function OccurrencesOfSubString(S, SubStr: String): Integer;
var
DSStr: String;
begin
if Pos(SubStr, S) = 0 then
Exit
else
DSStr := S;
Repeat
if Pos(SubStr, S) <> 0 then
Inc(Result);
Delete(DSStr, Pos(SubStr, DSStr), Length(Copy(DSStr, Pos(SubStr, DSStr), Length(SubStr))));
Until Pos(SubStr, DSStr) = 0;
end;