使用方法: fillright(inputstr,'A',10);//右填充 function fillRight( str :string; c :char;len : integer):string;stdcall; var i,lengs :integer; tmp:string; begin lengs :=length(str); tmp:=str; //大于等于,直接返回 if (lengs >= len) then begin result :=tmp; end else begin //从右侧填充 for i:=1 to (len-lengs) do begin tmp:=tmp+c; end; result:=tmp; end; end;
function FillRight(S: String; L: Integer):String; begin Result := S + StringOfChar(' ', L - Length(S)); end; 一个调用的例子: ShowMessage(FillRight('AAAAA', 10));
用Format很方便。 var S: string; begin S := 'AAAAA'; Writeln(Format('"%10s"', [S])); // " AAAAA" Writeln(Format('"%-10s"', [S])); // "AAAAA " end;
function fillRight( str :string; c :char;len : integer):string;stdcall;
var i,lengs :integer;
tmp:string;
begin
lengs :=length(str);
tmp:=str;
//大于等于,直接返回
if (lengs >= len) then
begin
result :=tmp;
end
else
begin
//从右侧填充
for i:=1 to (len-lengs) do
begin
tmp:=tmp+c;
end;
result:=tmp;
end;
end;
function FillRight(S: String; L: Integer):String;
begin
Result := S + StringOfChar(' ', L - Length(S));
end;
一个调用的例子:
ShowMessage(FillRight('AAAAA', 10));
var
S: string;
begin
S := 'AAAAA';
Writeln(Format('"%10s"', [S])); // " AAAAA"
Writeln(Format('"%-10s"', [S])); // "AAAAA "
end;