delphi 生成重复字符串

发布时间 2023-07-08 19:20:50作者: txgh

生成重复字符串

代码

重复字符或字符串

uses System.StrUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  //返回重复字符
  s := StringOfChar('A', 10);
  Memo1.Lines.Add(s);
  //返回重复字符串
  s := DupeString('ABC', 5);
  Memo1.Lines.Add(s);
end;

返回结果

AAAAAAAAAA
ABCABCABCABCABC

数字格式化补0或补空格

procedure TForm1.Button2Click(Sender: TObject);
var
  s: string;
begin
  s := '123.45';
  //10位,不足前补0
  Memo1.Lines.Add(StringOfChar('0', 10 - s.Length) + s);
  //10位,不足后补空格
  Memo1.Lines.Add(s + StringOfChar(' ', 10 - s.Length));
end;

返回结果

0000123.45
123.45    

生成层级结构

procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Lines.Add(DupeString('..', 1) + '1级');
  Memo1.Lines.Add(DupeString('..', 2) + '2级');
  Memo1.Lines.Add(DupeString('..', 3) + '3级');
end;

返回结果

..1级
....2级
......3级

方法

System.StringOfChar

function StringOfChar(Ch: WideChar; Count: Integer): UnicodeString; overload;
function StringOfChar(Ch: _AnsiChr; Count: Integer): _AnsiStr; overload;

Unit

System

返回具有指定数量的重复字符的字符串。

参数

Ch 指定的字符。

Count 重复次数。

System.StrUtils.DupeString

function DupeString(const AText: string; ACount: Integer): string;

Unit

System.StrUtils

返回指定重复次数的字符串。

参数

AText 指定的字符串。

Count 重复次数。