方法三:(推荐)(来自:道法自然)据说此函数速度最快
function GetSubStrNumC(const SubStr, S: widestring ): Integer;
var
I,X : Integer;
Len, LenSubStr: Integer;
begin
LenSubStr := Length(SubStr);
Result :=0;
I :=1;
LenSubStr := Length(SubStr);
Len := Length(S) - LenSubStr + 1;
while I <= Len do
begin
if S[I] = SubStr[1] then
begin
X := 1;
while (X < LenSubStr) and (S[I + X] = SubStr[X + 1]) do
Inc(X);
if (X = LenSubStr) then
begin
Result :=Result +1;
end ;
i:=i+X;
end else
Inc(I);
end;
end;
方法一:(来自万一老师)
function GetSubStrNum(const SubStr,Str: WideString): Integer;
var
pw: PWideChar;
i,SubLen: Integer;
begin
Result := 0;
pw := PWideChar(Str);
SubLen := Length(SubStr);
while i > 0 do
begin
i := Pos(SubStr, pw);
Inc(pw, i - 1 + SubLen);
if Boolean(i) then Inc(Result);
end;
end;
方法二:(来自:道法自然)
function GetSubStrNumB(const SubStr: WideString; Str: WideString): Integer; var b:Boolean; p:integer; begin Result := 0; b:=true; while b do begin p:=Pos(SubStr,Str);//避免两次测长 if p>0 then begin Str := Copy(Str, p+1,MaxInt );//Length(Str));用MaxInt比Length(Str));代价小得多 Result:=Result+1;// Inc(Result);//实际上inc要比+1要耗时些 end else b := false; end; end;