Fórum Verificar se em uma string existe algum inteiro... #197003
25/11/2003
0
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
Curtir tópico
+ 0Posts
25/11/2003
Carnette
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);
Gostei + 0
25/11/2003
Luizfernando777
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;
Gostei + 0
25/11/2003
Beppe
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
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)