1、TStringList 默认以 ','拆分字符
onst
constr :String = 'aaa,bbb,ccc,ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]); //aaa bbb ccc ddd
end;
2、通过 Delimiter 定义分割字符
const
constr :String = 'aaa\bbb\ccc\ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
const
constr :String = '"aaa"\"bbb"\"ccc"\"ddd"';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
3、增加 QuoteChar 属性 分割
const
constr :String = '|aaa|\|bbb|\|ccc|\|ddd|';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.QuoteChar := '|'; //QuoteChar。其默认值为:'"'(不包括单引号)
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
end;
4、通过 Names & Values & ValueFromIndex
const
constr :String = '0=aaa,1=bbb,2=ccc,3=ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to strs.Count-1 do
begin
ShowMessage(strs.Names[i]);
ShowMessage(strs.Values[strs.Names[i]]);
ShowMessage(strs.ValueFromIndex[i]);
end;
end;