子串在父串中出现的次数PosCount
function PosCount(const Substr, S: string): Integer;
var
vLen: Integer;
begin
vLen := Length(Substr);
if vLen = 0 then
Result := 0
else
Result := (Length(S) - Length(StringReplace(S, Substr, '', [rfReplaceAll]))) div vLen;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(IntToStr(PosCount('魂牵梦萦', '魂牵梦萦魂牵梦萦在需要魂牵梦萦')));//3
ShowMessage(IntToStr(PosCount('牵2', '魂牵2梦萦魂牵梦萦4梦梦梦魂牵2梦萦')));//2
ShowMessage(IntToStr(PosCount('2', '123254879935212')));//4
end;
var
vLen: Integer;
begin
vLen := Length(Substr);
if vLen = 0 then
Result := 0
else
Result := (Length(S) - Length(StringReplace(S, Substr, '', [rfReplaceAll]))) div vLen;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(IntToStr(PosCount('魂牵梦萦', '魂牵梦萦魂牵梦萦在需要魂牵梦萦')));//3
ShowMessage(IntToStr(PosCount('牵2', '魂牵2梦萦魂牵梦萦4梦梦梦魂牵2梦萦')));//2
ShowMessage(IntToStr(PosCount('2', '123254879935212')));//4
end;