procedure TForm1.Button1Click(Sender: TObject); const constr :String = '中文|english|123456||'; 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; end;
仁兄,还忘了释放 strs 补充liangyong007a((梦里有云,心中有天地)(探花秀)) procedure TForm1.Button5Click(Sender: TObject); const constr :String = '中文|english|123456||'; var strs :TStrings; i :Integer; begin strs := TStringList.Create; try strs.Delimiter := '|'; strs.DelimitedText := constr; for i := 0 to Strs.Count-1 do ShowMessage(Strs[i]); finally strs.Free; end; end;
function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings): Integer;
type TSplitArray=array of string; PSplitArray=^TSplitArray; TSplit=class(TStringList) public function Split(str:string;Delimiter:string):TSplitArray; function SplitStr(str:string;Delimiter:string):TSplitArray; end;{ TSplit }function TSplit.Split(str, Delimiter: string): TSplitArray; var i:integer; begin if length(Delimiter)>0 then self.Delimiter := Delimiter[1] else self.Delimiter := char(pchar('')); self.DelimitedText := str;
setLength(result,count); for i := 0 to count - 1 do begin result[i] := Strings[i]; end; end;function TSplit.SplitStr(str, Delimiter: string): TSplitArray; var temp:string; i:integer; ArraySize:integer; begin temp := str; ArraySize := 0; while pos(Delimiter,temp) > 0 do begin inc(ArraySize); setLength(result,ArraySize); result[high(result)] := copy(temp,1,pos(Delimiter,temp)-1); System.delete(temp,1,pos(Delimiter,temp)+length(Delimiter)-1); end; if temp<>'' then begin inc(ArraySize); setLength(result,ArraySize); result[high(result)] := temp; end; end; procedure TForm1.BitBtn3Click(Sender: TObject); var SplitArray:TSplitArray; split:TSplit; i:integer; begin split := TSplit.Create; SplitArray := split.Split('I,am,a,boy!',','); for i := low(SplitArray) to high(SplitArray) do showmessage(SplitArray[i]); SplitArray := split.SplitStr('I,,am,,a,,boy!',',,'); for i := low(SplitArray) to high(SplitArray) do showmessage(SplitArray[i]); split.Free; end;
不用这么复杂!更用不着动用TSTRINGLIST它老人家!Function x(s;string;index:integer;sp:char):String; var i,ipos,temppos,len:integer; begin i:=1; ipos:=0; temppos:=1; len:=length(s); while i<len do begin if s[i]=sp then begin inc(ipos); if ipos=index then begin result:=copy(s,tempos,i-tempos); break; end; temppos:=i; end; inc(i); end; end;
const
constr :String = '中文|english|123456||';
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;
end;
补充liangyong007a((梦里有云,心中有天地)(探花秀))
procedure TForm1.Button5Click(Sender: TObject);
const
constr :String = '中文|english|123456||';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
try
strs.Delimiter := '|';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
ShowMessage(Strs[i]);
finally
strs.Free;
end;
end;
Strings: TStrings): Integer;
strList:TStringList;ExtractStrings(['|'], [], PChar(str), strList)
1、使用TStringList的功能,传入字符串和分割符,返回一个一维动态数组
2、自己编写啊。传入字符串和分割“串”,循环POS,得到一个就增加动态数组长度并写入,再删除POS位置后的分割串及以前所有字符
TSplitArray=array of string;
PSplitArray=^TSplitArray;
TSplit=class(TStringList)
public
function Split(str:string;Delimiter:string):TSplitArray;
function SplitStr(str:string;Delimiter:string):TSplitArray;
end;{ TSplit }function TSplit.Split(str, Delimiter: string): TSplitArray;
var
i:integer;
begin
if length(Delimiter)>0 then
self.Delimiter := Delimiter[1]
else
self.Delimiter := char(pchar(''));
self.DelimitedText := str;
setLength(result,count);
for i := 0 to count - 1 do
begin
result[i] := Strings[i];
end;
end;function TSplit.SplitStr(str, Delimiter: string): TSplitArray;
var
temp:string;
i:integer;
ArraySize:integer;
begin
temp := str;
ArraySize := 0; while pos(Delimiter,temp) > 0 do
begin
inc(ArraySize);
setLength(result,ArraySize);
result[high(result)] := copy(temp,1,pos(Delimiter,temp)-1);
System.delete(temp,1,pos(Delimiter,temp)+length(Delimiter)-1);
end;
if temp<>'' then
begin
inc(ArraySize);
setLength(result,ArraySize);
result[high(result)] := temp;
end;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
SplitArray:TSplitArray;
split:TSplit;
i:integer;
begin split := TSplit.Create;
SplitArray := split.Split('I,am,a,boy!',',');
for i := low(SplitArray) to high(SplitArray) do
showmessage(SplitArray[i]); SplitArray := split.SplitStr('I,,am,,a,,boy!',',,');
for i := low(SplitArray) to high(SplitArray) do
showmessage(SplitArray[i]);
split.Free;
end;
var
i,ipos,temppos,len:integer;
begin
i:=1;
ipos:=0;
temppos:=1;
len:=length(s);
while i<len do
begin
if s[i]=sp then
begin
inc(ipos);
if ipos=index then
begin
result:=copy(s,tempos,i-tempos);
break;
end;
temppos:=i;
end;
inc(i);
end;
end;