Testar se em uma string contem um numero

Delphi

03/08/2005

Como faço para teste se em uma string contem um numero(integer);

ex.:


resultado:= testanumero(´10´);

function testanumero(texto:string):boolean;
begin
//aqui está a duvida
if texto = numero then
result:= True;
end;


Atualmente estou utilizando a função [b:7a578fbcf8]strtoint[/b:7a578fbcf8] e trato com [b:7a578fbcf8]execpt[/b:7a578fbcf8]


Sistemald

Sistemald

Curtidas 0

Respostas

Rjun

Rjun

03/08/2005

resultado := testanumero(´10´); 

function testanumero(texto: string): boolean; 
var
  i: integer;
begin 
  result := false;
  for i := 1 to length(texto) do
    if texto[i] in [´0´..´9´] then 
    begin
      result:= True; 
      exit;
    end;
end;



GOSTEI 0
Fx|hand

Fx|hand

03/08/2005

Eu criei uma função pra diminuir a propria função do SysUtils(D7).. q é a
[b:751de81eea]TryStrToInt()[/b:751de81eea] .... e ficou assim...

{MINHA FUNÇÃO}

Function IsNum(S:String): Boolean; 
var 
OutVar: Integer; 
begin
  Result := TryStrToInt(s,OutVar);
end;

{FIM DELA}


procedure TForm1.Button1Click(Sender: TObject); 
begin
  ShowMessage (BooltoStr(IsNum(Edit1.Text)));
end;

{Ela manda um resultado em Boolean... vc poderia trocar e fazer ela retornar o valor ´0´ (em integer) se for o seu caso... ou se a String for um numero, ele Retorna o numero q foi lançado pra função.... a função ficaria assim}

{MINHA FUNÇÃO}

Function IsNum2(S:String): Integer;
var
OutVar: Integer;
begin
  Val(S, OutVar, OutVar);
  Result := OutVar;
end;

{FIM DELA}

procedure TForm1.Button1Click(Sender: TObject); 
begin
  ShowMessage (IntToStr(IsNum(Edit1.Text)));
end;



GOSTEI 0
Sistemald

Sistemald

03/08/2005

Era isso mesmo que eu precisava

TryStrtoInt

Valeu galera


GOSTEI 0
Kotho

Kotho

03/08/2005

Vc pode usar, também, StrToIntDef(texto, valor_se_erro)


GOSTEI 0
POSTAR