可以用最笨的方法,一个字符一个字符的比较,帮你写一个吧,效率很低,如果你需要处理大量数据的话再提出来,帮你优化procedure TForm1.Button1Click(Sender: TObject); var str: string; sl: TStringList; i: integer; function Split(s: string; c1, c2: Char): TStringList; var i: integer; begin Result := TStringList.Create; while (Pos(c1, s)>0) or (Pos(c2, s)>0) do begin for i:=1 to Length(s) do begin if (s[i]=c1) or (s[i]=c2) then begin Result.Add(Copy(s, 1, i-1)); s := Copy(s, i+1, Length(s)); break; end; end; end; if Length(s)>0 then Result.Add(s); end; begin str := '11/08/2005-RC410-SB450-PTRC-GC-00'; sl := TStringList.Create; sl := Split(str, '/', '-'); for i:=0 to sl.Count-1 do ShowMessage(sl[i]); FreeAndNil(sl); end;
var SL: TStringList; i: Integer; begin SL := TStringList.Create; try SL.Delimiter := '/'; SL.DelimitedText := '11/08/2005-RC410-SB450-PTRC-GC-00'; SL.Delimiter := '-'; SL.DelimitedText :=SL.Text; for i:= 0 to SL.Count -1 do begin Edit1.Text := Edit1.Text + SL.Strings[i];//SL.Strings[i]即索求 end finally SL.Free; end; end;
var s: string begin s := '深圳 最低温度10 最高温度20 天气晴朗'; while Pos(' ', s) > 0 do begin s := StringReplace(s, ' ', ' ', [rfReplaceAll]); end; s := StringReplace(s, ' ', #13#10, [rfReplaceAll]); //下面再利用TStringList处理 end;
var
str: string;
sl: TStringList;
i: integer;
function Split(s: string; c1, c2: Char): TStringList;
var
i: integer;
begin
Result := TStringList.Create;
while (Pos(c1, s)>0) or (Pos(c2, s)>0) do
begin
for i:=1 to Length(s) do
begin
if (s[i]=c1) or (s[i]=c2) then
begin
Result.Add(Copy(s, 1, i-1));
s := Copy(s, i+1, Length(s));
break;
end;
end;
end;
if Length(s)>0 then Result.Add(s);
end;
begin
str := '11/08/2005-RC410-SB450-PTRC-GC-00';
sl := TStringList.Create;
sl := Split(str, '/', '-');
for i:=0 to sl.Count-1 do
ShowMessage(sl[i]);
FreeAndNil(sl);
end;
SL: TStringList;
begin
SL := TStringList.Create;
try
SL.Text := '11/08/2005-RC410-SB450-PTRC-GC-00';
SL.Text := StringReplace(SL.Text, '/', #13#10, [rfReplaceAll]);
SL.Text := StringReplace(SL.Text, '-', #13#10, [rfReplaceAll]);
//此时SL[0]='11' SL[1] = '08'....
finally
SL.Free;
end;
end;
SL: TStringList;
i: Integer;
begin
SL := TStringList.Create;
try
SL.Delimiter := '/';
SL.DelimitedText := '11/08/2005-RC410-SB450-PTRC-GC-00'; SL.Delimiter := '-';
SL.DelimitedText :=SL.Text; for i:= 0 to SL.Count -1 do
begin
Edit1.Text := Edit1.Text + SL.Strings[i];//SL.Strings[i]即索求
end
finally
SL.Free;
end;
end;
s: string
begin
s := '深圳 最低温度10 最高温度20 天气晴朗';
while Pos(' ', s) > 0 do
begin
s := StringReplace(s, ' ', ' ', [rfReplaceAll]);
end;
s := StringReplace(s, ' ', #13#10, [rfReplaceAll]);
//下面再利用TStringList处理
end;