如何以最高的效率格式化字符窜 string1:='20080116180004';目标字符窜(时间格式):'2008-01-16 18:00:04' 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写了一天程序后,脑细胞死了好多,不想去想了function TForm1.formatLikeDatetime(S:string):string;var str:string;begin str:=copy(S,1,4)+'-'+copy(S,5,2)+'-' +copy(S,7,2)+' '+copy(S,9,2)+':' +copy(S,11,2)+':'+copy(S,13,2); result:=str;end; 写个入门级的,呵呵function Tform1.formatstr(datestring:string):string;var year,month,day,hour,minute,second:string;begin if datestring='' then result:='' else try year:=copy(datestring,1,4); month:=copy(datestring,5,2); day:=copy(datestring,7,2); hour:=copy(datestring,9,2); minute:=copy(datestring,11,2); second:=copy(datestring,13,2); result:=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; except application.MessageBox('格式化出错','提示',64); end;end; 这个怎么样:function GetFormatDateTime(const S: string): string;type TDTFormat = packed record Y: array [0..3] of Char; M: array [0..1] of Char; D: array [0..1] of Char; H: array [0..1] of Char; MS: array [0..1] of Char; S: array [0..1] of Char; end;var P: ^TDTFormat; begin P := Pointer(S); if Length(S) >= SizeOf(TDTFormat) then Result := P.Y + '-' + P.M + '-' + P.D + ' ' + P.H + ':' + P.MS + ':' + P.S else Result := ''end; 我也凑个热闹,看这样写如何:procedure TForm1.Button1Click(Sender: TObject);var str: String;begin str := '20080116180004'; insert('-',str,5); insert('-',str,8); insert(' ',str,11); insert(':',str,14); insert(':',str,17); showmessage(str);end; 4楼的不错,输出时用Format('%s-%s-%s %s:%s:%s',[P.Y,P.M,P.D,P.H,P.MS,P.S]); 先分配空间,再COPY。Format耗时不少的。 var string1,string2:String; p1,p2:PChar; iCount:Integer;begin string1:='20080116180004'; SetLength(String2,19); p1:=Pchar(String1); p2:=PChar(String2); //YYYY iCount := 4; move( p1^,p2^,iCount); inc(p1,iCount); inc(p2,iCount); //- p2^ := '-'; inc(p2); //MM iCount := 2; move( p1^,p2^,iCount); inc(p1,iCount); inc(p2,iCount); //- p2^ := '-'; inc(p2); //DD move( p1^,p2^,iCount); inc(p1,iCount); inc(p2,iCount); //(Space) p2^ := #$20;// inc(p2); //hh move( p1^,p2^,iCount); inc(p1,iCount); inc(p2,iCount); //: p2^ := ':';// inc(p2); //nn move( p1^,p2^,iCount); inc(p1,iCount); inc(p2,iCount); //: p2^ := ':';// inc(p2); //ss move( p1^,p2^,iCount); ShowMessage(string2);end; ODAC 连接 Oracle unicode字符集问题 表结构类的设计思路 图形合并(救命啊,大侠们,立即结帖,在线等) 关于用控件进行远程数据库连接的问题。 急急急!!!如何将阳历转换成阴历?在线等 aiirii(ari-爱的眼睛) 接分 哪里能下载QuickReport v3.5或者更高版本? delphi 中命令行参数如何取得????上者有分!!!^_^ 怎样去掉一个窗体的标量栏并且可让窗体自由移动 Delphi中能否动态添加控件? [新手提问]delphi的TMemo中删除问题? 如何解决绑定记录集实时更新存在安全隐患
var
str:string;
begin
str:=copy(S,1,4)+'-'+copy(S,5,2)+'-'
+copy(S,7,2)+' '+copy(S,9,2)+':'
+copy(S,11,2)+':'+copy(S,13,2);
result:=str;
end;
var
year,month,day,hour,minute,second:string;
begin
if datestring='' then
result:=''
else
try
year:=copy(datestring,1,4);
month:=copy(datestring,5,2);
day:=copy(datestring,7,2);
hour:=copy(datestring,9,2);
minute:=copy(datestring,11,2);
second:=copy(datestring,13,2);
result:=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
except
application.MessageBox('格式化出错','提示',64);
end;end;
function GetFormatDateTime(const S: string): string;
type
TDTFormat = packed record
Y: array [0..3] of Char;
M: array [0..1] of Char;
D: array [0..1] of Char;
H: array [0..1] of Char;
MS: array [0..1] of Char;
S: array [0..1] of Char;
end;
var
P: ^TDTFormat;
begin
P := Pointer(S);
if Length(S) >= SizeOf(TDTFormat) then
Result := P.Y + '-' + P.M + '-' + P.D + ' ' + P.H + ':' + P.MS + ':' + P.S
else
Result := ''
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str: String;
begin
str := '20080116180004';
insert('-',str,5);
insert('-',str,8);
insert(' ',str,11);
insert(':',str,14);
insert(':',str,17);
showmessage(str);
end;
输出时用Format('%s-%s-%s %s:%s:%s',[P.Y,P.M,P.D,P.H,P.MS,P.S]);
Format耗时不少的。
string1,string2:String;
p1,p2:PChar;
iCount:Integer;
begin
string1:='20080116180004';
SetLength(String2,19);
p1:=Pchar(String1);
p2:=PChar(String2); //YYYY
iCount := 4;
move( p1^,p2^,iCount);
inc(p1,iCount);
inc(p2,iCount);
//-
p2^ := '-';
inc(p2); //MM
iCount := 2;
move( p1^,p2^,iCount);
inc(p1,iCount);
inc(p2,iCount);
//-
p2^ := '-';
inc(p2); //DD
move( p1^,p2^,iCount);
inc(p1,iCount);
inc(p2,iCount);
//(Space)
p2^ := #$20;//
inc(p2); //hh
move( p1^,p2^,iCount);
inc(p1,iCount);
inc(p2,iCount);
//:
p2^ := ':';//
inc(p2); //nn
move( p1^,p2^,iCount);
inc(p1,iCount);
inc(p2,iCount);
//:
p2^ := ':';//
inc(p2); //ss
move( p1^,p2^,iCount);
ShowMessage(string2);
end;