//我做了个简单的例程,可以实现你所需的功能,在form上分别放一个TEdit,TButton,TListbox,其属性均为默认值,在button1的Onclick事件中书写代码:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; ListBox1: TListBox; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;//本函数用于判断所传入的字符串是不是数字格式的 function isnum(str:String):Boolean; implementation{$R *.DFM}function isnum(str:String):Boolean; begin result:=true; try strtofloat(str); except result:=false; end; end;procedure TForm1.Button1Click(Sender: TObject); Var tempstr:String; begin listbox1.Clear; tempstr:=trim(edit1.Text); while pos(',',tempstr)>0 do begin if isnum(copy(tempstr,1,pos(',',tempstr)-1)) then listbox1.items.add(copy(tempstr,1,pos(',',tempstr)-1)); tempstr:=copy(tempstr,pos(',',tempstr)+1,length(tempstr)-pos(',',tempstr)); end; if (pos(',',tempstr)<=0) and (tempstr<>'') then if isnum(tempstr) then listbox1.Items.Add(tempstr); end;end.
function ConvertString(s: String): String; var i: Integer; b,e: Integer; begin Result := ''; b := -1; i := 1; if s[Length(s)] <> ',' then s := s + ','; while i <= Length(s) do begin case s[i] of '0'..'9': begin if b = -1 then b := 0; b := b * 10 + Byte(s[i]) - Byte('0'); end ',': begin if b = -1 then raise Exception.Create('Unknown format'); Result := Result + IntToStr(b) + ','; b := -1; end; '-': begin if b = -1 then raise Exception.Create('Unknown format'); e := 0; Inc(i); while (i <= Length(s)) and (s[i] in ['0'..'9']) do begin e := e * 10 + Byte(s[i]) - Byte('0'); Inc(i); end; Dec(i); if e < b then raise Exception.Create('Unknown format'); while b <= e do begin Result := Result + IntToStr(b) + ','; Inc(b); end; end; else raise Exception.Create('Unknown format'); end; Inc(i); end; end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;//本函数用于判断所传入的字符串是不是数字格式的
function isnum(str:String):Boolean;
implementation{$R *.DFM}function isnum(str:String):Boolean;
begin
result:=true;
try
strtofloat(str);
except
result:=false;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
Var
tempstr:String;
begin
listbox1.Clear;
tempstr:=trim(edit1.Text);
while pos(',',tempstr)>0 do
begin
if isnum(copy(tempstr,1,pos(',',tempstr)-1)) then
listbox1.items.add(copy(tempstr,1,pos(',',tempstr)-1));
tempstr:=copy(tempstr,pos(',',tempstr)+1,length(tempstr)-pos(',',tempstr));
end;
if (pos(',',tempstr)<=0) and (tempstr<>'') then
if isnum(tempstr) then
listbox1.Items.Add(tempstr);
end;end.
var
i: Integer;
b,e: Integer;
begin
Result := '';
b := -1;
i := 1;
if s[Length(s)] <> ',' then s := s + ',';
while i <= Length(s) do
begin
case s[i] of
'0'..'9':
begin
if b = -1 then b := 0;
b := b * 10 + Byte(s[i]) - Byte('0');
end
',':
begin
if b = -1 then raise Exception.Create('Unknown format');
Result := Result + IntToStr(b) + ',';
b := -1;
end;
'-':
begin
if b = -1 then raise Exception.Create('Unknown format');
e := 0;
Inc(i);
while (i <= Length(s)) and (s[i] in ['0'..'9']) do
begin
e := e * 10 + Byte(s[i]) - Byte('0');
Inc(i);
end;
Dec(i);
if e < b then raise Exception.Create('Unknown format');
while b <= e do
begin
Result := Result + IntToStr(b) + ',';
Inc(b);
end;
end;
else raise Exception.Create('Unknown format');
end;
Inc(i);
end;
end;