procedure TFrmmain.Button1Click(Sender: TObject); var s : String ; begin s := 'ab' ; if Length(s) < 5 then s := Format('%s ',[s]) ; ShowMessage(s + ' ' + IntToStr(Length(s))) ; //ab 5 end;
function myFormatStr(In:string):string; const s := ' '; var L : integer; begin L := Length(In); if L<5 then result := In + Copy(s,1,5-L); end;
if Length(s) < 5 then while Length(s) < 5 do s := s + ' ' ;
借 liang_z(千山一刀之忍者神龟) 老师函数 :)function myFormatStr(In:string):string; begin if Length(In) < 5 then while Length(In) < 5 do In := In + ' ' ; Result := In ; end;
function myFormatStr(In:string):string; const s := ' '; var L : integer; begin L := Length(In); if L<5 then result := In + Copy(s,1,5-L) else result := Copy(In,1,5); end;
这个好象用Format是完不成的!
var
s : String ;
begin
s := 'ab' ;
if Length(s) < 5 then s := Format('%s ',[s]) ;
ShowMessage(s + ' ' + IntToStr(Length(s))) ; //ab 5
end;
const
s := ' ';
var
L : integer;
begin
L := Length(In);
if L<5 then
result := In + Copy(s,1,5-L);
end;
while Length(s) < 5 do
s := s + ' ' ;
begin
if Length(In) < 5 then
while Length(In) < 5 do
In := In + ' ' ;
Result := In ;
end;
const
s := ' ';
var
L : integer;
begin
L := Length(In);
if L<5 then
result := In + Copy(s,1,5-L)
else
result := Copy(In,1,5);
end;
我的意思是输出的字符串式定长的,比如前边的可能是3个,2个,但输出的都是5个长度。不够用空格补上!