一个极简单的问题,但是思路上就是堵在那,循环迷了(100分)

T

tt55

Unregistered / Unconfirmed
GUEST, unregistred user!
我就是想分析一个不定长的字符串中的数字,
用逗号分开的字符串的数字到一个stringlist中去
如edit1.text := '22,222,56,123,323,1345,.....';
把数字分离开放到stringlist中保存,循环就写不好了,请大家帮一下忙吧,分开就给分
 
呵呵,告诉你一个臭办法:
你把字符串中的","全换成回车符,然后load到stringlist中去,反正stringlist会把自动
在回车处分行的
说错了不要生气,生气老得快 *_*
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1508804
这个应该与你的需求有关,jsxjd的回答很好你不妨借鉴一下。
 
最简单的方法:
StringList.QuoteChar := ',';
StringList.CommaText := Edit1.Text;
就可以了。
 
比较原始的方法,可以参考一下,建议用Yukin的方法
procedure TForm1.Button1Click(Sender: TObject);
var s,s1:string;
i:integer;
begin
s:='311,222,222,333,7,8,999';
for i:=1 to length(s) do
begin
if (s=',') then
begin
//此处加入增加的语句
showmessage(s1);
s1:='';
end else
begin
s1:=s1+s;
if i=length(s) then
begin
//此处加入增加的语句
showmessage(s1);
end;
end;
end;
end;
 
有现成的函数为什么不用呢?
参见Delphi帮助
function ExtractStrings(Separators, WhiteSpace: TSysCharSet;
Content: PChar;
Strings: TStrings): Integer;
Description
Use ExtractStrings to fill a string list with the substrings of the null-terminated string specified by Content.
Separators is a set of characters that are used as delimiters, separating the substrings. Carriage returns, newline characters, and quote characters (single or do
uble) are always treated as separators. Separators are ignored when inside a quoted string until the final end quote. (Note that quoted characters can appear in a quoted string if the quote character is do
ubled.)
WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the begin
ning of a string.
Content is the null-terminated string to parse into substrings.
Strings is a string list to which all substrings parsed from Content are added. The string list is not cleared by ExtractStrings, so any strings already in the string list are preserved.
ExtractStrings returns the number of strings added to the Strings parameter.
Note: ExtractStrings do
es not add empty strings to the list.
 
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
S:string;
begin
List:=TStringList.Create;
try
S:=Edit1.Text;
while Pos(',', S)<>0 do
begin
List.Add(Copy(S, 1, Pos(',', S)-1));
S:=Copy(S, Pos(',', S)+1, Length(S));
end;
List.Add(S);
finally
FreeAndNil(List);
end;
end;
 
如果要自己做可以:
strTemp := Edit1.Text;
While Length(strTemp) > 0 do
begin
if Pos(',',strTemp) > 0 then
begin
StringList.AddString(Copy(strTemp,1,Pos(',',strTemp)-1));
Delete(strTemp,1,Pos(',',strTemp));
end;
end;
只是给你个提示,你可以优化一下代码。
 
多人接受答案了。
 
一个调用的例子:
ExtractStrings([','], [' '], PChar(Edit1.Text), Memo1.Lines);
注意:会忽略空的字符串,字符串开头的空白会被略去,但后边的不会
如:abcd,_a123__,mm,,_nnp3__ //其中_表示空格,结果为:
abcd
a123__
mm
nnp3__
 
我一直这样用,可以用字符串分割
procedure TForm1.stringToTstrings(Src: string;
S: string;
Ts_s: Tstrings);
var
F: Word;
begin
if Ts_s=nil then
exit;
Ts_s.Clear;
f := POS(Src, S);
while (f <> 0) do
begin
Ts_s.Add(COPY(S, 1, F - 1));
// s := COPY(S, F + length(src), length(s));
Delete(s, 1, f);
F := POS(Src, s);
end;
if Length(s) > 0 then
Ts_s.Add(s);
end;
 
顶部