主题:初学者请教。。排序问题。
只差最后一步了。函数实现如:在Edit框内输入“6,4,12,5”,按Button后Edit框内输出“4,5,6,12”。先将字符串内‘,’取出后排序,请问如何将‘,’插入字符串中。
procedure TForm1.SplitString(AInstring: string; Separator: char;
var AOutString: Tstringlist);
var
sAlone: String;
iPos: Integer;
begin
try
AOutString.Clear;
if Length(AInstring) = 0 then Exit;
iPos := Pos(Separator, AInstring);
while iPos <> 0 do
begin
sAlone := Copy(AInstring, 1, iPos - 1);
AOutString.Add(sAlone);
Delete(AInstring, 1, iPos);
iPos := Pos(Separator, AInstring);
end;
if Length(AInstring) > 0 then
AOutString.Add(AInstring);
finally
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t: string;
slTmp, slTmp1: Tstringlist;
m, n , i, c: Integer;
begin
try
slTmp := Tstringlist.Create;
if length(Edit1.Text) = 0 then Exit;
Splitstring(Edit1.Text, ',', slTmp);
for m := 0 to slTmp.Count - 2 do
begin
for n := m + 1 to slTmp.Count - 1 do
begin
if StrToIntDef(slTmp.Strings[m],0) > StrToIntDef(slTmp.Strings[n],0) then
begin
t := slTmp.Strings[m];
slTmp.Strings[m] := slTmp.Strings[n];
slTmp.Strings[n] := t;
end;
end;
end;
c := slTmp.Count;
for i := 0 to c * 2 - 1 do
begin
Insert(slTmp, ',', i + 2);
end;
edit1.Text:= slTmp.Text;
finally
slTmp.free;
end;
请问如何将‘,’插入数组中,Insert好像不行。。。谢谢。。
procedure TForm1.SplitString(AInstring: string; Separator: char;
var AOutString: Tstringlist);
var
sAlone: String;
iPos: Integer;
begin
try
AOutString.Clear;
if Length(AInstring) = 0 then Exit;
iPos := Pos(Separator, AInstring);
while iPos <> 0 do
begin
sAlone := Copy(AInstring, 1, iPos - 1);
AOutString.Add(sAlone);
Delete(AInstring, 1, iPos);
iPos := Pos(Separator, AInstring);
end;
if Length(AInstring) > 0 then
AOutString.Add(AInstring);
finally
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t: string;
slTmp, slTmp1: Tstringlist;
m, n , i, c: Integer;
begin
try
slTmp := Tstringlist.Create;
if length(Edit1.Text) = 0 then Exit;
Splitstring(Edit1.Text, ',', slTmp);
for m := 0 to slTmp.Count - 2 do
begin
for n := m + 1 to slTmp.Count - 1 do
begin
if StrToIntDef(slTmp.Strings[m],0) > StrToIntDef(slTmp.Strings[n],0) then
begin
t := slTmp.Strings[m];
slTmp.Strings[m] := slTmp.Strings[n];
slTmp.Strings[n] := t;
end;
end;
end;
c := slTmp.Count;
for i := 0 to c * 2 - 1 do
begin
Insert(slTmp, ',', i + 2);
end;
edit1.Text:= slTmp.Text;
finally
slTmp.free;
end;
请问如何将‘,’插入数组中,Insert好像不行。。。谢谢。。