procedure TForm1.Button1Click(Sender: TObject); var Str,Sum:String; i:integer; StrList:Array[0..9998] of string; begin DateTimeToString(Str,'YYMMDD',Now); for i:=1 to 9999 do begin if (i>=1) and (i<=9) then StrList[i-1]:=Str+'000'+IntToStr(i); if (i>=10) and (i<=99) then StrList[i-1]:=Str+'00'+IntToStr(i); if (i>=100) and (i<=999) then StrList[i-1]:=Str+'0'+IntToStr(i); if (i>=1000) and (i<=9999) then StrList[i-1]:=Str+IntToStr(i); end; ShowMessage(StrList[10]); end;
我把它放到一个string数组里。用的时候调用就可以了。
For I := 1 To 9999 Do Begin X := FormatDateTime( 'YYMMDD', Now ) + Format( '%.4u', [ I ] ); End;
to ksaiy(真爱不是取代,而是心中永恒的存在) ,也许是我说的复杂了,每次需要生成这样的字符串时,他的最后四位的基数是已知的,比如:0301,生成时只需要在最后四位上加上1,到0302,怎么办呢?
已知str := '0301' X := FormatDateTime( 'YYMMDD', Now ) + Format( '%.4u', [ StrToInt(str) + 1 ] );
如果这个值是已知的话,你可以这样做: procedure TForm1.Button1Click(Sender: TObject); var Str,Sum:String; Int:integer; begin Int:=100;//已知的. DateTimeToString(Str,'YYMMDD',Now); Sum:=Str+Format('%.4u',[Int+1]);//增加1. ShowMessage(Sum); end;
var
Str,Sum:String;
i:integer;
StrList:Array[0..9998] of string;
begin
DateTimeToString(Str,'YYMMDD',Now);
for i:=1 to 9999 do
begin
if (i>=1) and (i<=9) then
StrList[i-1]:=Str+'000'+IntToStr(i);
if (i>=10) and (i<=99) then
StrList[i-1]:=Str+'00'+IntToStr(i);
if (i>=100) and (i<=999) then
StrList[i-1]:=Str+'0'+IntToStr(i);
if (i>=1000) and (i<=9999) then
StrList[i-1]:=Str+IntToStr(i);
end;
ShowMessage(StrList[10]);
end;
Begin
X := FormatDateTime( 'YYMMDD', Now ) + Format( '%.4u', [ I ] );
End;
X := FormatDateTime( 'YYMMDD', Now ) + Format( '%.4u', [ StrToInt(str) + 1 ] );
procedure TForm1.Button1Click(Sender: TObject);
var
Str,Sum:String;
Int:integer;
begin
Int:=100;//已知的.
DateTimeToString(Str,'YYMMDD',Now);
Sum:=Str+Format('%.4u',[Int+1]);//增加1.
ShowMessage(Sum);
end;