如何取得动态的字符串(50分)

  • 主题发起人 主题发起人 刀剑如梦
  • 开始时间 开始时间

刀剑如梦

Unregistered / Unconfirmed
GUEST, unregistred user!
我有个动态的字符串,比如《123》《234》《456》《789》...中间没有空格,要取123 234 456 789 ,怎么办?而且动态的字符串还会还会增加,《》之间的字符不同,其他的一样!请各位帮忙!
 
用AnsiReplaceText(const AText, AFromText, AToText: string)函数,如AnsiReplaceText('《123》', '《', ''); //把《去掉,即123》
AnsiReplaceText('《123》', '《', ''); //把》去掉,即123
 
同意楼上老兄的意见,其详细代码如下:

假设,楼主利用两个TEdit 控件,Edit1是输入,而Edit2是输出则实现代码如下:

procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text:=AnsiReplaceText((AnsiReplaceText(edit1.Text,'《','')),'》','');
end;

经过测试...
 
var
s:string;
l:TStrings;
begin
s:= '《123》《234》《456》《789》';
l:=TStringList.Create;
ExtractStrings(['《','》'],[],PChar(s),l);
end;
 
来自:轻舞肥羊, 时间:2003-6-12 0:51:00, ID:1944809
var
s:string;
l:TStrings;
begin
s:= '《123》《234》《456》《789》';
l:=TStringList.Create;
ExtractStrings(['《','》'],[],PChar(s),l);
end;

楼上的代码没有问题,只不过,楼主所说的的任意字符,不可能在声明时把S变量写满不可估量的字符把?
 
比较通用的函数:
Function Form1.GetStr(Splitter:char;Text:string):string;
var
BeginPos,EndLen:integer;
begin
Text:=Trim(Text);
BeginPos:=Pos(Splitter,Text)+1; //《123》 | (123) | abc-123
EndLen:=Length(Text)-Pos(Splitter,Text)-2; //'《' 中文符号占两个字符,故减2,'('西文符号:-1,不配对使用的'-'不减
Result:=Copy(Text,BeginPos,EndLen);
end;

调用方法:
self.caption:=GetStr('《',edit1.text);


 
s := '《123》《234》《456》《789》...';
StringReplace(s, '》《', #13#10, [rfReplaceAll]);
StringReplace(s, '《', '', [rfReplaceAll]);
StringReplace(s, '》', '', [rfReplaceAll]);
st := TStringList.Create;
st.Text := s;
然后用个循环就可以一个一个读出来了
 
我这儿有段代码,要把下列格式的数据转化为后面的形式
希望能对你有参考

'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.
 
谢谢各位仁兄的帮忙,小弟不甚感激!
 
后退
顶部