我写了一个大概的算法,但还不够精确,你自己再改一下procedure TForm1.Button1Click(Sender: TObject); var strTest,RowStr,Result: String; strList: TStringList; i,temp: Integer; begin strList := TStringList.Create; strTest := '1,2,3,4,5,7,9,33,907,8888,8889,8890'; //得到第一个数字 temp := StrToInt(LeftStr(strTest,Pos(',',strTest)-1)); strList.Text := WrapText(strTest,#13,[','],1); for i := 0 to strList.Count - 1 do begin //得到下一个数字 if RightStr(strList.Strings[i],1) = ',' then RowStr := LeftStr(strList.Strings[i],Length(strList.Strings[i])-1) else begin RowStr := strList.Strings[i]; end; //比较 if (StrToInt(RowStr) > temp) and (StrToInt(RowStr) - temp = 1) then begin Delete(Result,Length(Result),1); Result := Result + '-'; temp := StrToInt(RowStr); end else begin temp := StrToInt(RowStr); Result := Result + RowStr + ','; end; end;//end for strList.Free; ShowMessage(Result+IntToStr(temp)); end;我的Email:[email protected]
var
strTest,RowStr,Result: String;
strList: TStringList;
i,temp: Integer;
begin
strList := TStringList.Create;
strTest := '1,2,3,4,5,7,9,33,907,8888,8889,8890'; //得到第一个数字
temp := StrToInt(LeftStr(strTest,Pos(',',strTest)-1)); strList.Text := WrapText(strTest,#13,[','],1); for i := 0 to strList.Count - 1 do
begin
//得到下一个数字
if RightStr(strList.Strings[i],1) = ',' then
RowStr := LeftStr(strList.Strings[i],Length(strList.Strings[i])-1)
else
begin
RowStr := strList.Strings[i];
end; //比较
if (StrToInt(RowStr) > temp) and (StrToInt(RowStr) - temp = 1) then
begin
Delete(Result,Length(Result),1);
Result := Result + '-';
temp := StrToInt(RowStr);
end
else
begin
temp := StrToInt(RowStr);
Result := Result + RowStr + ',';
end;
end;//end for
strList.Free;
ShowMessage(Result+IntToStr(temp));
end;我的Email:[email protected]