D6 版本筆記 Split
D6 版本筆記
procedure TForm1.Button1Click(Sender: TObject);
var
load, tmpstr, tmpstr1, tmpstr2: TStringList;
str_alert: string;
begin
load := TStringList.Create;
tmpstr := TStringList.Create;
tmpstr1 := TStringList.Create;
tmpstr2 := TStringList.Create;
load.LoadFromFile('D:\LOG\1EG07G32902.csv');
//Data Info 'Process Start_for_Recipe,15.CrPtAu30K,Time,12:01'
ExtractStrings([','], [' '], PChar(load.Strings[0]), tmpstr);
str_alert := tmpstr.Strings[0];
ShowMessage('tmpstr = ' + str_alert); //Process Start_for_Recipe
tmpstr1.Delimiter := ',';
tmpstr1.DelimitedText := load.Strings[0];
str_alert := tmpstr1.Strings[0];
ShowMessage('tmpstr1 = ' + str_alert); //Process
//D6 版本tmpstr1 指定切割符號仍無效切割需要的INFO
//tmpstr2透過取代方式,再用STRLIST預設切割符號即可取得所需資訊
load.Text := StringReplace(load.Text, ' ', '_', [rfReplaceAll, rfIgnoreCase]);
tmpstr2.CommaText := load.Strings[0];
str_alert := tmpstr2.Strings[0];
ShowMessage('tmpstr2 = ' + str_alert); //Process_Start_for_Recipe
end;
更新時間20160617
此方法會遇到空白的問題 ~自動切割的問題
解法1可先取代掉空白在做切割使用時再取代回來
方法2 ExtractStrings
如下範例
procedure TForm1.Button1Click(Sender: TObject);
var ss:TStringlist;
i: integer;
sparax, sqlcmd:string;
begin
sparax:='a,b,c d,e';
ss:=TStringlist.Create;
SS.Delimiter := Chr(44);
ss.DelimitedText := sparax; // 要被分割的字串
for i:=0 to SS.Count -1 do
begin
messageDlg(ss[i], mtError, [mbOk], 0);
end;
// 輸出
// a
// b
// c
// d
// e
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
ASource: PChar;
AStrings: TStringList;
begin
try
//分割 //過濾 //資料內容
// function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar;
// Strings: TStrings): Integer;
ASource := 'a,b,c d,e,###A';
AStrings := TStringList.Create;
ExtractStrings([','], ['#'], ASource, AStrings);
//........................
for i:=0 to AStrings.Count -1 do
begin
messageDlg(AStrings[i], mtError, [mbOk], 0);
end;
finally
AStrings.Free;
end;
// 輸出
// a
// b
// c d
// e
// A
end;
方法三 網友寫function
function SplitString(const Source,ch:string):TStringList;
var
temp:String;
i:Integer;
begin
Result:=TStringList.Create;
//如果是空字串的話
if Source=''
then exit;
temp:=Source;
i:=pos(ch,Source);
while i<>0 do
begin
Result.add(copy(temp,0,i-1));
Delete(temp,1,i);
i:=pos(ch,temp);
end;
Result.add(temp);
end;
procedure TForm1.Button3Click(Sender: TObject);
var i: integer;
tstring: TStringList;
begin
tstring := SplitString('192.168.1.1 .a a','.');
for i:=0 to tstring.Count -1 do
begin
messageDlg(tstring[i], mtError, [mbOk], 0);
end;
end;
大家一起加入blogads 賺零用錢!!