unit Unit14; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.StrUtils, Vcl.StdCtrls; type TForm14 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form14: TForm14; implementation {$R *.dfm} uses QString; function GetStrNum(const SubStr, S: string): Integer; var I: Integer; begin Result := 0; I := PosEx(SubStr, S, 1); if I > 0 then Inc(Result) else Exit; while (I > 0) and (I < Length(S)) do begin I := PosEx(SubStr, S, I + 1); if I > 0 then Inc(Result); end; end; function StrPostime(Str:string;P:string):integer; var i,n:integer; begin n:=0; for i:=1 to length(str) do begin if copy(str,i,length(p))=P then n:=n+1; end; StrPostime:=n; end; function SubStrConut(mStr: string; mSub: string): Integer; { 返回子字符串出现的次数 } begin Result := (Length(mStr) - Length(StringReplace(mStr, mSub, '', [rfReplaceAll]))) div Length(mSub); end; { SubStrConut } function StrPosCount(subs:string;source:string):integer; var Str : string; begin Result := 0; str := source; while Pos(Subs,Str)<>0 do begin Delete(Str,Pos(Subs,Str),Length(Subs)); Inc(Result); end; end; function InStrTimes(const AShortStr,ASourceStr: string): Integer; var MyArrS: TArray<string>; begin MyArrS := ASourceStr.Split([AShortStr], ExcludeEmpty); Exit(Length(MyArrS)-1); end; procedure TForm14.Button1Click(Sender: TObject); var n: Cardinal; I: Integer; begin //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin LeftStrCount('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的',false); end; n := GetTickCount - n; Text := IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin RightStrCount('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的',false); end; n := GetTickCount - n; Text := Text + IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin GetStrNum('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗'); end; n := GetTickCount - n; Text := Text + IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin StrPostime('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的'); end; n := GetTickCount - n; Text := Text + IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin SubStrConut('我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗','爱你的'); end; n := GetTickCount - n; Text := Text + IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin StrPosCount('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗'); end; n := GetTickCount - n; Text := Text + IntToStr(n) + ' - '; //------------------------------------ n := GetTickCount; for I := 0 to 1000000 do begin InStrTimes('爱你的','我靠你的我爱你的你不爱我的我不爱你的真心的很爱你的爱你的还需要理由吗'); end; n := GetTickCount - n; Text := Text + IntToStr(n); end; end.
可见第三个是最快的。
隔离出来第三个备份下:
function InStrTimes(const ASubStr,ASourceStr: string): Integer; var I: Integer; begin Result := 0; I := PosEx(ASubStr, ASourceStr, 1); if I > 0 then begin Inc(Result); end else begin Exit; end; while (I > 0) and (I < Length(ASourceStr)) do begin I := PosEx(ASubStr, ASourceStr, I + 1); if I > 0 then begin Inc(Result); end; end; end;
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/p/4360339.html