我这儿有段代码,要把下列格式的数据转化为后面的形式
希望能对你有参考
'02',
TO_DATE('10-Nov-2001 11:49:34','DD-MON-YYYY HH24:MI:SS'),
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
'02',
TO_DATE('10-Nov-2001 11:49:34','DD-MON-YYYY HH24:MI:SS'),
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
'02',
TO_DATE('10-Nov-2001 11:49:34','DD-MON-YYYY HH24:MI:SS'),
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
转化为:
'02',
'2001-11-10 11:49:34',
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
'02',
'2001-11-10 11:49:34',
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
'02',
'2001-11-10 11:49:34',
'4021',
'553201',
'41034107',
0,
5,
0,
'1',
'410304',
注意日期形式的变化
我的代码如下,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdGlobal, ComCtrls, ExForms;
type
TMainFrm = class(TLiForm)
Button1: TButton;
Memo1: TMemo;
CheckBox1: TCheckBox;
ProgressBar1: TProgressBar;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit3: TEdit;
Button3: TButton;
OpenDialog1: TOpenDialog;
Label4: TLabel;
Label5: TLabel;
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button100Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function FindStr(const S: String; const S1: String;
var iStart: Integer; IgnoreCase: Boolean = False): Integer;
end;
var
MainFrm: TMainFrm;
mms: String = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
implementation
{$R *.dfm}
procedure TMainFrm.Button3Click(Sender: TObject);
var
fn, fn2: String;
fs, fw: TFileStream;
s, ss, ps, t: String;
m, n, k: Integer;
tm, tn: Integer;
i: Integer;
sb, se: String;
dt: TDateTime;
st, sc: TStringList;
s1, s2: String;
Cnt: Integer;
begin
Cnt := 0;
fn := Edit3.Text;
ProgressBar1.Position := 0;
Label2.Caption := '';
Label5.Caption := '';
if not FileExists(fn) then
begin
ShowMessage('文件不存在.');
Exit;
end;
st := nil;
fs := nil;
sc := nil;
try
st := TStringList.Create;
sc := TStringList.Create;
sc.CommaText := mms;
sc.CaseSensitive := False;
Memo1.Lines.Clear;
fs := TFileStream.Create(fn, fmOpenRead);
fn2 := ChangeFileExt(fn, '.txt');
SetLength(s, fs.Size);
ProgressBar1.Max := fs.Size;
fs.Read(s[1], Length(s));
i := 1;
sb := 'TO_DATE(''';
se := ''',''DD-MON-YYYY HH24:MI:SS'')';
tm := 1; tn := 0;
SetLength(SS, Length(S) * 2);
while i <= Length(s) do
begin
m := FindStr(S, sb, i);
if m > 0 then
n := FindStr(S, se, i)
else
n := 0;
if (m > 0) and (n > 0) then
begin
//ss := ss + Copy(S, tm, m - tm);
//高效替换
Move(S[tm], SS[tn + 1], m - tm);
tn := tn + m - tm;
ps := Copy(S, m + Length(sb), n - m - Length(sb));
k := Pos(' ', ps);
if k = 0 then k := Length(ps) + 1;
s1 := Copy(ps, 1, k - 1);
s2 := Copy(ps, k + 1, Length(ps));
t := StringReplace(s1, '-', #13#10, [rfReplaceAll]);
st.Text := t;
st.Move(2, 0);
st.Move(2, 1);
k := sc.IndexOf(st[1]) + 1;
if k < 1 then raise Exception.Create('月份错误');
t := st[0] + '-' + Format('%.2d', [k]) + '-' + st[2] + ' ' + s2;
dt := StrToDateTime(t);
t := '''' + FormatDateTime('yyyy-mm-dd hh:mm:ss', dt) + '''';
//ss := ss + t;
//高效替换
Move(t[1], SS[tn + 1], Length(t));
tn := tn + Length(t);
tm := n + Length(se);
ProgressBar1.Position := tm;
Label2.Caption := Format('%d/%d', [tm, Length(S)]);
Label2.Update;
if CheckBox1.Checked then
begin
Memo1.Lines.Add(ps + ' : ' + t);
end;
Cnt := Cnt + 1;
Label5.Caption := IntToStr(Cnt);
Label5.Update;
end;
end;
//ss := ss + Copy(s, tm, Length(S) - tm + 1);
//高效替换
Move(s[tm], SS[tn + 1], Length(S) - tm + 1);
tn := tn + Length(S) - tm + 1;
SetLength(SS, tn);
tm := Length(S);
ProgressBar1.Position := tm;
Label2.Caption := Format('%d/%d', [tm, Length(S)]);
Label2.Update;
fw := TFileStream.Create(fn2, fmCreate);
fw.Write(ss[1], Length(ss));
fw.Free;
ShowMessage('保存成功: '#13#10 + fn2);
finally
if Assigned(st) then FreeAndNil(st);
if Assigned(fs) then fs.Free;
if Assigned(sc) then sc.Free;
end;
end;
function TMainFrm.FindStr(const S, S1: String; var iStart: Integer;
IgnoreCase: Boolean): Integer;
var
SA, SB: String;
i, mat: Integer;
begin
Result := 0;
if iStart < 1 then iStart := 1;
if not IgnoreCase then
begin
i := iStart;
mat := 1;
while i <= Length(S) do
begin
if S = S1[mat] then
begin
mat := mat + 1;
end
else
begin
i := i - mat + 1;
mat := 1;
end;
i := i + 1;
if mat > Length(S1) then
begin
iStart := i;
Result := i - mat + 1;
Break;
end;
end;
end
else
begin
SA := LowerCase(S1);
SB := UpperCase(S1);
i := iStart;
mat := 1;
while i <= Length(S) do
begin
if (S = SA[mat]) or (S = SB[mat]) then
begin
mat := mat + 1;
end
else
begin
i := i - mat + 1;
mat := 1;
end;
i := i + 1;
if mat > Length(S1) then
begin
iStart := i;
Result := i - mat + 1;
Break;
end;
end;
end;
iStart := i;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
begin
Memo1.Lines.Clear;
end;
procedure TMainFrm.Button100Click(Sender: TObject);
var
st: TStringList;
s: String;
s1, s2: String;
begin
st := TStringList.Create;
s := '10-Nov-2001 11:49:34';
{
sav := LongDateFormat;
LongDateFormat := 'dd-mmm-yyyy hh:mm:ss';
dt := StrToDateTime(s);
LongDateFormat := sav;
}
st.Text := StringReplace(s, ' ', #13#10, [rfReplaceAll]);
s1 := st[0];
s2 := st[1];
st.Text := StringReplace(s1, '-', #13#10, [rfReplaceAll]);
st.Move(2, 0);
st.Move(2, 1);
ShowMessage(IntToStr(st.Count));
st.Free;
end;
procedure TMainFrm.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit3.Text := OpenDialog1.FileName;
end;
end.