procedure TForm1.Button1Click(Sender: TObject); var Source:String; Dest:TStrings; Mid:TStrings; i,j,k:integer; begin Source := '1,24,56'; Mid := TStringList.Create; Dest := TStringList.Create; Dest.Clear; Mid.Delimiter:=','; Mid.DelimitedText := Source; for i := 1 to Length(Mid[0]) do begin for j := 1 to Length(Mid[1]) do begin for k := 1 to Length(Mid[2]) do begin ShowMessage(Mid[0][i]+Mid[1][j]+Mid[2][k]); Dest.Add(Mid[0][i]+Mid[1][j]+Mid[2][k]); end; end; end; Dest.SaveToFile('C:\test.txt'); Mid.Free; Dest.Free; end;
试试这个procedure TForm1.Button1Click(Sender: TObject); var s,ss,s1,ss1:string; str:Tstrings; i,j,m,n,k,l,ii,jj:Integer; b:Boolean; begin s:=Trim(self.Edit1.Text); str:=TStringList.Create; str.Clear; self.ListBox1.Items.Clear; ExtractStrings([','],[],PChar(s),str); i:=str.Count; if i=0 then Exit; l:=0; s1:=str.Strings[0]; ii:=Length(s1); for jj:=1 to ii do begin k:=0; b:=True; while b do begin m:=j+1; k:=k+1; ss1:=''; for n:=1 to i-1 do begin s:=str.Strings[n]; if Length(s)<k then Continue; ss1:=ss1+s[k]; end; if ss1<>'' then b:=True else b:=False; ss:=s1[jj]+ss1; self.ListBox1.Items.Add(ss); end; end; end;
//经过大家的提示,自己写出了该算法,贴上来供大家参考一下。//大体思路:采用递归的方法,不管中间结果怎么样,最终结果都是中间结果加上当前 //字符串中的各个字符function SplitStrToArray(aStr: String): TStrings; var ss :string; strLst, TempStrLst1, TempStrLst2 :Tstrings; i,j,k :Integer; begin strLst:=TStringList.Create; strLst.Clear; TempStrLst1 := TStringList.Create; TempStrLst1.Clear; TempStrLst2 := TStringList.Create; TempStrLst2.Clear; ExtractStrings([','],[],PChar(aStr),strLst); i := strLst.Count; for i := 0 to strLst.Count - 1 do begin TempStrLst2.Clear; //每次进行字符串连接时清空 ss := strLst[i]; //获取当前进行连接的字符串 if i = 0 then //递归的第一个条件 begin for j := 0 to Length(ss) - 1 do begin TempStrLst2.Add(ss[j + 1]); end; end else for j := 0 to TempStrLst1.Count - 1 do //已经得到的中间递归结果 begin for k := 0 to Length(ss) - 1 do begin TempStrLst2.Add(TempStrLst1.Strings[j] + ss[k + 1]); end; end; TempStrLst1.Clear; TempStrLst1.Assign(TempStrLst2); //中间结果赋值给中间变量 end; Result := TempStrLst2; end;
var
Source:String;
Dest:TStrings;
Mid:TStrings;
i,j,k:integer;
begin
Source := '1,24,56';
Mid := TStringList.Create;
Dest := TStringList.Create;
Dest.Clear;
Mid.Delimiter:=',';
Mid.DelimitedText := Source;
for i := 1 to Length(Mid[0]) do
begin
for j := 1 to Length(Mid[1]) do
begin
for k := 1 to Length(Mid[2]) do
begin
ShowMessage(Mid[0][i]+Mid[1][j]+Mid[2][k]);
Dest.Add(Mid[0][i]+Mid[1][j]+Mid[2][k]);
end;
end;
end;
Dest.SaveToFile('C:\test.txt');
Mid.Free;
Dest.Free;
end;
var
s,ss,s1,ss1:string;
str:Tstrings;
i,j,m,n,k,l,ii,jj:Integer;
b:Boolean;
begin
s:=Trim(self.Edit1.Text);
str:=TStringList.Create;
str.Clear;
self.ListBox1.Items.Clear;
ExtractStrings([','],[],PChar(s),str);
i:=str.Count;
if i=0 then
Exit;
l:=0; s1:=str.Strings[0];
ii:=Length(s1);
for jj:=1 to ii do
begin
k:=0;
b:=True;
while b do
begin
m:=j+1;
k:=k+1;
ss1:='';
for n:=1 to i-1 do
begin
s:=str.Strings[n];
if Length(s)<k then
Continue;
ss1:=ss1+s[k];
end;
if ss1<>'' then
b:=True
else
b:=False;
ss:=s1[jj]+ss1;
self.ListBox1.Items.Add(ss);
end;
end;
end;
//字符串中的各个字符function SplitStrToArray(aStr: String): TStrings;
var
ss :string;
strLst, TempStrLst1, TempStrLst2 :Tstrings;
i,j,k :Integer;
begin
strLst:=TStringList.Create;
strLst.Clear;
TempStrLst1 := TStringList.Create;
TempStrLst1.Clear;
TempStrLst2 := TStringList.Create;
TempStrLst2.Clear;
ExtractStrings([','],[],PChar(aStr),strLst);
i := strLst.Count;
for i := 0 to strLst.Count - 1 do
begin
TempStrLst2.Clear; //每次进行字符串连接时清空
ss := strLst[i]; //获取当前进行连接的字符串
if i = 0 then //递归的第一个条件
begin
for j := 0 to Length(ss) - 1 do
begin
TempStrLst2.Add(ss[j + 1]);
end;
end
else
for j := 0 to TempStrLst1.Count - 1 do //已经得到的中间递归结果
begin
for k := 0 to Length(ss) - 1 do
begin
TempStrLst2.Add(TempStrLst1.Strings[j] + ss[k + 1]);
end;
end;
TempStrLst1.Clear;
TempStrLst1.Assign(TempStrLst2); //中间结果赋值给中间变量
end;
Result := TempStrLst2;
end;