- 注册时间
- 2011-3-6
- 最后登录
- 1970-1-1
该用户从未签到
|
分割一个字符串如s,,df s.d+g,我用[',,','+',' ']作为分隔符ExtractStrings就不能用“,,”,好像只能单字符的。现在写了个函数用来分割,用能用了,但好像没地方释放StringList和tempslist,帮我看看怎么改
function SplitString(Source: string; Deli: Array of string): tstringList;
stdcall;
var
u, J, h, m: byte;
tstr: string;
Delis: Array of string;
StringList, tempslist: tstringList;
begin
StringList := tstringList.Create;
tempslist := tstringList.Create;
for u := 0 to High(Deli) do
begin
tstr := '';
J := Pos(Deli, Source);
if J > 0 then
begin
h := length(Deli);
setLength(Delis, High(Deli) - u);
for m := 0 to High(Deli) - u - 1 do
Delis[m] := Deli[1 + m];
tstr := copy(Source, 1, J - 1);
tempslist := SplitString(tstr, Delis);//用其他分割符分割这个字符
if tempslist.Count = 0 then 。。没有分出来则原来的附进去
tempslist.Add(tstr);
for m := 0 to tempslist.Count - 1 do
StringList.Add(tempslist[m]);
Source := copy(Source, J + h, length(Source) - J);//剩余的进行分割
tempslist.Free; //tempslist := tstringList.Create;
tempslist:=nil;
tempslist := SplitString(Source, Deli);//free nil了这么还能赋值???I基础差!
if tempslist.Count = 0 then//没有分出来则原来的附进去
tempslist.Add(Source);
for J := 0 to tempslist.Count - 1 do
begin
StringList.Add(tempslist[J]);
end;
Result := StringList;//返回结果直接退出,因为已经分完了,上面第一个FOR不需再执行下去
//StringList.Free;这里释放有问题
Exit;
end;
end;
Result := StringList;//最后返回结果搞得好像不像递归了
//这里释放也不行
end; |
|