zt:DupeString 与 StringOfChar 的区别
StringOfChar 是反复 “字符” 成 “字符串” 的函数;
DupeString 是反复 “字符串” 成 “新字符串” 的函数;
StringOfChar 来自 System 单元, 可以直接使用;
DupeString 来自 StrUtils 单元, 使用时需要 uses StrUtils;
如果仅仅是反复 “字符”, 当然应该用 StringOfChar , 它是用汇编代码实现的, 速度会稍好一些.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses StrUtils; {DupeString 来自这个单元}
procedure TForm1.FormCreate(Sender: TObject);
var
str: string;
begin
str := System.StringOfChar(‘A’, 5);
ShowMessage(str); {AAAAA}
str := StrUtils.DupeString(‘A’, 5);
ShowMessage(str); {AAAAA}
str := StrUtils.DupeString(‘ABC’, 5);
ShowMessage(str); {ABCABCABCABCABC}
end;
{如果只是反复字符, StringOfChar 肯定会更快一些; 速度测试:}
procedure TForm1.Button1Click(Sender: TObject);
var
t1,t2: Cardinal;
i: Integer;
begin
t1 := GetTickCount;
for i := 0 to 1000000 do DupeString(‘A’, 5);
t1 := GetTickCount – t1;
t2 := GetTickCount;
for i := 0 to 1000000 do StringOfChar(‘A’, 5);
t2 := GetTickCount – t2;
ShowMessageFmt(‘DupeString: %d; StringOfChar: %d’, [t1,t2]);
end;
end.
没事,别怕,这是签名→→┃ 青山幽谷笛声扬,白鹤振羽任翱翔。往事前尘随风逝,携手云峰隐仙乡。 ┃