求一个简单的函数,传一个字符串参数,把字符串中非数字的字符全部去掉,返回一个全是数字的字符串。

解决方案 »

  1.   

    function numstr(s:String):String;
    var i:integer;
    begin
      result:='';
      for i:=1 to length(s) do
          begin
            if (Copy(s,i,1)>='0') and (Copy(s,i,1)<='9') then
               result:=result+Copy(s,i,1);            
          end;
    end;
      

  2.   

    function tform1.getNum(str:string):string;
    var
      tempstr:string;
      i:integer;
    begin
      tempstr:= '';
      for i:=0 to length(str) do
      if str[i] in ['0','1','2','3','4','5','6','7','8','9'] then
        tempstr:= tempstr+str[i];
      result:= tempstr;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    edit2.Text := getNum(edit1.Text );
    end;