字符串处理难题,大家帮看下,被绕进去了我(100分)

  • 主题发起人 主题发起人 寒芳轻
  • 开始时间 开始时间

寒芳轻

Unregistered / Unconfirmed
GUEST, unregistred user!
字符串由一些从小到大的数字组成,有的连续,有的不连续,每个数字后面都有逗号:
如:1,2,3,5,6,8,11,
要得到这样的结果:1-3,5-6,8,11
请达人帮看下,分不多,解决就结贴。。。
 
不懂你在问什么?
你是要问用什么计算规律可以得到这样结果吗?

i:integer;
i:=0;
i:=i+3;
<=i 有结果,1,2,3即(1-3)
i:=i+3;
<=i 有结果,4不存在所以只有5,6即(5-6)
i:=i+3;
<=i 有结果,7,9都不存在,所以只有8即(8)
i:=i+3;
<=i 有结果,10,11,12,因为10,12都不存在,所以只有11,即(11);

思路就是这样了,每次加3,得到3个数,祛除不存在的,就是结果.
不过你问的问题不清,举出来的例子也不够,因此我只能是不完全归纳.
 
谢谢wuyongzhen关注,
我得问题就是要得到数字和-,还有逗号组成得新得字符串,
数字连续得就用-线取头头尾组合,不连续得就用逗号间隔,
源字符串是一些连续得数字和不连续得数字组成,并且已经是从小到打排过许了
我得操作是这样得,吧数字放近数组里
var arr1:arrar of integer
a,b,c,i:integer;
str1,str2:string;
sf:boolean;
begin
sf:=false;
str2:=inttostr(arr1[0]);
for i:=0 to arr1.count-1 do
begin
b:=arr1;
if (b+1)=arr1[i+1] then
begin
if not sf then
begin
str2:=str2+inttostr(arr1)+'-';
end else
begin
str2:=str2+'-';
end;
sf:=true;
end else
begin
if sf then
begin
str2:=str2+inttostr(arr1)+',';
end else
begin
str2:=str2+',';
end;
sf:=false;
end;
end;
end;
如果数组里放得是 3 5 6 11
得到得结果是 3,5-6,,没了11
 
抛砖引玉了。

procedure TForm1.Button1Click(Sender: TObject);
var lst_temp: TStrings;
li_Start, li_Current, li_Prior, li_Count: Integer;
ls_temp: String;
begin
lst_temp := TStringList.Create;

try
lst_temp.Delimiter := ',';
lst_temp.DelimitedText := Edit1.Text;

ls_temp := '';
li_Start := StrToInt(lst_temp.Strings[0]);
for li_Count := 1 to lst_temp.Count-1 do
if lst_temp.Strings[li_Count] <> '' then
begin
li_Prior := StrToInt(lst_temp.Strings[li_Count - 1]);
li_Current := StrToInt(lst_temp.Strings[li_Count]);
//大于1,则不连续了
if (li_Current - li_Prior > 1) then
begin
if li_Start <> li_Prior then
ls_temp := ls_temp + IntToStr(li_Start) + '-' + IntToStr(li_Prior) + ','
else
ls_temp := ls_temp + IntToStr(li_Start) + ',';

li_Start := li_Current;
end;
//最后一个要特殊判断一下
if (li_Count = lst_temp.Count-2) then
begin
if li_Start <> li_Current then
ls_temp := ls_temp + IntToStr(li_Start) + '-' + IntToStr(li_Current) + ','
else
ls_temp := ls_temp + IntToStr(li_Current) + ',';

li_Start := li_Current;
end;
end;

Edit2.Text := ls_temp;
finally
lst_temp.Free;
end;
end;
 
function Get(const Source: string): string;
var
Src: PChar;

function GetInteger(var V: Integer): Boolean;
begin
V := 0;
Result := False;
while Src^ in [' ', ',', #13, #10, #9] do Inc(Src);
while Src^ in ['0'..'9'] do
begin
Result := True;
V := V * 10 + (Ord(Src^) - $30);
Inc(Src);
end;
end;

var
IsGet: Boolean;
V, StartInt, NextInt: Integer;
begin
Result := '';
Src := PChar(Source);
IsGet := GetInteger(V);

while IsGet do
begin
IsQuit := False;
StartInt := V;
NextInt := StartInt + 1;

while True do
begin
IsGet := GetInteger(V);
if NextInt = V then
Inc(NextInt)
else
break;
end;

if (StartInt + 1) = NextInt then
Result := Result + Format('%d,', [StartInt])
else
Result := Result + Format('%d-%d,', [StartInt, NextInt - 1]);
end;
if Result <> '' then
Delete(Result, Length(Result), 1);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
function AddText(const ABegin,AEnd: Integer; s: TStrings): String;
begin
if ABegin = AEnd then
Result := s.Strings[ABegin]
else
Result := s.Strings[ABegin] + '-' + s.Strings[AEnd];
end;
var
s1,s2: TStrings;
i,iBegin: Integer;
al: array of Integer;
begin
s1 := TStringList.Create;
s2 := TStringList.Create;
try
s1.CommaText := '1,2,3,5,6,8,11';
SetLength(al,s1.Count);
for i := 0 to High(al) do
al := StrToInt(s1.Strings);

iBegin := 0;
for i := 1 to High(al) do begin
if (al = al[i - 1] + 1) then Continue;
s2.Add(AddText(iBegin,i - 1,s1));
iBegin := i;
end;
s2.Add(AddText(iBegin,i - 1,s1));
Caption := s2.CommaText;
finally
s1.Free;
s2.Free;
end;
end;
 
大概是这样:
function GetNumStr(str: string): string;
//返回连接数量(1: 就是只有自己)
function GetSeriesNum(StrList: TStrings; StartIndex: Integer): Integer;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{如:1,2,3,5,6,8,11,
要得到这样的结果:1-3,5-6,8,11}

function TForm1.GetNumStr(str: string): string;
const
SPLIT_STR = ',';
var
i, iSeriesNum: Integer;
strList: TStrings;
begin
Result := EmptyStr;
strList := TStringList.Create;
try
//拆分字串到TStringList
ExtractStrings([SPLIT_STR], [], PChar(str), strList);
iSeriesNum := 0;
i := 0;
//for i := 0 to strList.Count - 1 do
while i <= strList.Count - 1 do
begin
iSeriesNum := GetSeriesNum(strList, i);
Case iSeriesNum of
0: Exit;
1:
begin
Result := Result + strList.Strings;
Inc(i);
end;
else
Result := Result + Format('%s-%d',
[strList.Strings, StrToIntDef(strList.Strings, 0) + iSeriesNum - 1]);
Inc(i, iSeriesNum);
end;

Result := Result + ', ';
end;
finally
strList.Free;
end;
end;

function TForm1.GetSeriesNum(StrList: TStrings; StartIndex: Integer): Integer;
var
i, StartNum: Integer;
begin
Result := 0;
with StrList do
begin
if Count < StartIndex then Exit;
StartNum := StrToIntDef(Strings[StartIndex], 0);
for i := StartIndex to Count - 1 do
if StrToIntDef(Strings, 0) > StartNum + i - StartIndex then
Exit
else
Inc(Result);

end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
memInfo.Lines.Add(GetNumStr('1,2,3,5,6,8,11,13,17,18,19,20'));
end;
 
多人接受答案了。
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部