L
luoyanqing119
Unregistered / Unconfirmed
GUEST, unregistred user!
'TG1,GK1-GK5,T1,r6,p7-p8'如何分解为'TG1,GK1,GK2,GK3,GK4,GK5,T1,r6,p7,p8,KK8-12'---------------'-'这个符号是连接符号,要的是合理的算法 原贴地址为:http://www.delphibbs.com/delphibbs/dispq.asp?lid=3978441 ------------------------------------------------------------------------------放飞兄的解法:function ExtStr(s, e: AnsiString): AnsiString;var Q: AnsiString; I: Integer;begin I := 1; repeat Inc(I) until S in ['0'..'9']; Q := Copy(S, 1, I - 1); Result := S; for I := StrToIntDef(Copy(S, I, MaxInt), 0) + 1 to StrToIntDef(Copy(E, I, MaxInt), 0) - 1 do Result := Result + ',' + Q + IntToStr(I); Result := Result + ',' + E;end;procedure TForm1.Button2Click(Sender: TObject);var sl: TStringList; S: AnsiString; I: Integer;begin sl := TStringList.Create; try s := 'TG1,GK1-GK5,T1,r6,p7-p8'; sl.CommaText := s; sl.NameValueSeparator := '-'; for I := 0 to sl.Count - 1 do begin if sl.Names <> '' then sl := ExtStr(sl.Names, sl.ValueFromIndex); end; sl.QuoteChar := ' '; sl.Delimiter := ','; Memo1.Lines.Add(sl.DelimitedText) finally sl.Free end;end;这次得到的是你要的结果了Memo1的内容是一个连续的字符串,如果你想每个处理后的字符串占一行,那么你只需要将 sl.Delimiter := ',';下面的 Memo1.Lines.Add(sl.DelimitedText);替换为 sl.CommaText := sl.DelimitedText; Memo1.Lines.AddStrings(sl);就可以了 -------------------------希望还有好的解法(效率和代码简洁性方面)