Fórum Verificar se em uma string existe algum inteiro... #197003

25/11/2003

0

Amigos...

Alguém sabe se existe alguma função que retorne true ou false caso uma string que é passada contém algum número inteiro (no início, meio ou fim)?

Desde já agradeço

Mariana


Mariana

Mariana

Responder

Posts

25/11/2003

Carnette

function ExisteInt(Texto:String): Boolean; {Testa se em uma string existe um numero inteiro valido ou não} var i:integer; begin try i := StrToInt(Texto); Result := True; except Result := False; end; end; end.


Na chamada voce usaria algo como:

For x:= 1 to lenght(string) do if ExisteInt(Copy(string,x,1)) then ShowMesage(´Existe um inteiro na posição ´+ IntToStr(x);


Responder

Gostei + 0

25/11/2003

Luizfernando777

ou
function TForm1.VerNumero(Value:String):Boolean;
var
I : Integer;
begin
Result := False;
for I := 0 to 9 do
begin
if Pos(IntToStr(I),Value) <> 0 then
begin
Result := True;
Exit;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if VerNumero(Edit1.Text) = True then
Label1.Caption := ´Verdade´ else
if VerNumero(Edit1.Text) = False then
Label1.Caption := ´Falso´;
end;


Responder

Gostei + 0

25/11/2003

Beppe

Esta rotina reconhece o primeiro número integral não-negativo que for encontrado.

function ContainsInt(const S: String; out Value: Integer): Boolean;
var
  P, T: PChar;
  Val: Integer;
begin
  Result := False;
  if S <> ´´ then
  begin
    P := Pointer(S);
    T := P + Length(S);
    Val := 0;
    repeat
      if P^ in [´0´..´9´] then
      begin
        repeat
          Val := Val * 10 + (Ord(P^) - Ord(´0´));
          Inc(P);
        until [´0´..´9´];
        Result := True;
        Break;
      end;
      Inc(P);
    until P >= T;
    Value := Val;
  end;
end;


Chame assim:
var
Numero: Integer;
begin
if ContainsInt(Edit1.Text, Numero) then
begin
// existe um número, o valor dele está em Numero
end else
// não existe...
end;

Ataliba


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar