How to check if a variable is integer?

I am using Inno Setup and want to check with Pascal Script if the string variable is Integer (only 0-9, without hex). I made this function:

function IsInt(s: string): boolean;
var
  i, len: Integer;
begin
  len := length(s);

  if len = 0 then
    result := false
  else
  begin
    result := true;
    for i := 1 to len do
    begin
      if not (s[i] in ['0'..'9']) then  !!! ERROR HERE !!!
      begin
        result := false;
        exit;
      end;
    end;
  end;
end; 

But the compiler raises an error:

Closing square bracket (']') expected.

How to fix it?

If I changed the line to this:

  if not (s[i] in ['0','1','2','3','4','5','6','7','8','9']) then

It matches, but if the code is executing, it gives this error:

Runtime Error - Invalid Type.

What to do?

+4
source share
2 answers

Instead of using kits, you can just do a simple range test like

IF (s[i] < '0') OR (s[i] > '9') THEN
   ...
+3
source

From the Pascal script documentation

Prototype: function StrToIntDef (s: string; def: Longint): Longint;

: StrToInt , S . S , StrToInt , Def.

def -1, , -1.

+2

Source: https://habr.com/ru/post/1543859/


All Articles