自己做一个函数吧 //alen 你需要的字符串长度 //astr 当前要加0的字串 //那么add0(4,'22')='0022' function add0(alen:integer;astr:string):string var i:integer; begin for i:=1 to alen-length(astr) do astr:='0'+astr; result:=astr; end;
No.2 procedure TForm1.Button2Click(Sender: TObject); begin Edit1.Text := Format('%-.4d', [12]); end;
例如Format('%4.4d',N) N:为数字 就是把N转换为四位数的字符串,不足的左边补零
SourceStr是你的源串,ResultStr是你要的结果,strLength是你规定的长度。 代码: ResultStr:string; //最后结果 tempStr:string; //中间结果 i:integer; tempStr:=SourceStr;for i:=1 to strLength do tempStr:='0'+tempStr;ResultStr:=copy(tempStr,length(SourceStr)+1,strLength);
// 自定义函数:字符串添加前导指定的字符满足一定的长度 function fill_str(cc_str:string;cc_width:integer;cc_fill:char):string; var cc_len,iii,ln_width:integer; dc_str:string; begin dc_str:=''; //lc_width:=cc_width; cc_len:= length(cc_str) ; if (cc_len < cc_width) then begin ln_width:=cc_width - cc_len; for iii:=1 to ln_width do dc_str:=dc_str+cc_fill; fill_str:=dc_str+cc_str; end else fill_str:=cc_str; end;
//alen 你需要的字符串长度
//astr 当前要加0的字串
//那么add0(4,'22')='0022'
function add0(alen:integer;astr:string):string
var i:integer;
begin
for i:=1 to alen-length(astr) do
astr:='0'+astr;
result:=astr;
end;
Edit1.Text := FloatFormat('0000', 12);
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.Text := Format('%-.4d', [12]);
end;
代码:
ResultStr:string; //最后结果
tempStr:string; //中间结果
i:integer;
tempStr:=SourceStr;for i:=1 to strLength do
tempStr:='0'+tempStr;ResultStr:=copy(tempStr,length(SourceStr)+1,strLength);
function fill_str(cc_str:string;cc_width:integer;cc_fill:char):string;
var cc_len,iii,ln_width:integer;
dc_str:string;
begin
dc_str:='';
//lc_width:=cc_width;
cc_len:= length(cc_str) ;
if (cc_len < cc_width) then
begin
ln_width:=cc_width - cc_len;
for iii:=1 to ln_width do
dc_str:=dc_str+cc_fill; fill_str:=dc_str+cc_str;
end
else
fill_str:=cc_str;
end;
xuemei(梅) ??? 《《《 女孩子???