[fpc-pascal] Verify if string is a valid number

Michael Van Canneyt michael at freepascal.org
Sun Nov 23 18:00:12 CET 2008



On Sun, 23 Nov 2008, Felipe Monteiro de Carvalho wrote:

> Hello,
> 
> I would like to verify if a string is a valid number, as in StrToInt
> will work when applied to it. The obvious way is making a try
> statement:
> 
>       try
>         StrToInt(TokenStr);
>         AdicionarToken(tNumeroLiteral, TokenStr);
>       except
>         AdicionarToken(tId, TokenStr);
>       end;
> 
> but I don't like this solution, because a lot of exceptions will be
> caused by normal execution of my program, which will disturb debugging
> it ...
> 
> It's easy to build such a routine, but I was just thinking that maybe
> there is one already somewhere in the RTL or FCL ... anyone know?

If TryStrToInt(s,i) then
  Writeln('S has a valid number:',I);

Or 

  Val(S,I,C);
  If C=0 then  
   Writeln('S has a valid number:',I);

Michael.





More information about the fpc-pascal mailing list