Is there a method in Delphi to check if a string is a number without raising an exception?
its for int parsing.
and an exception will raise if one use the
try
StrToInt(s);
except
//exception handling
end;
Is there a method in Delphi to check if a string is a number without raising an exception?
its for int parsing.
and an exception will raise if one use the
try
StrToInt(s);
except
//exception handling
end;
var
s: String;
iValue, iCode: Integer;
...
val(s, iValue, iCode);
if iCode = 0 then
ShowMessage('s has a number')
else
ShowMessage('s has not a number');
TryStrToInt
. And it requires at least two local variables. –
Landlady TryStrToInt
should be available in D7. Any reason why you accepted this solution and not the one posted by bassfriend? –
Landlady TryStrToInt
is in D7, as well as its friends TryStrToInt64
, TryStrToBool
, TryStrToFloat
, TryStrToCurr
, TryStrToDate
, TryStrToTime
and TryStrToDateTime
. Seems to me that the OP has accepted the wrong answer. –
Hattiehatton function TryStrToInt(const S: string; out Value: Integer): Boolean;
TryStrToInt converts the string S, which represents an integer-type number in either decimal or hexadecimal notation, into a number, which is assigned to Value. If S does not represent a valid number, TryStrToInt returns false; otherwise TryStrToInt returns true.
To accept decimal but not hexadecimal values in the input string, you may use code like this:
function TryDecimalStrToInt( const S: string; out Value: Integer): Boolean;
begin
result := ( pos( '$', S ) = 0 ) and TryStrToInt( S, Value );
end;
TryStrToInt
returns true even for xFF
, not only $FF
. –
Springing var
s: String;
iValue, iCode: Integer;
...
val(s, iValue, iCode);
if iCode = 0 then
ShowMessage('s has a number')
else
ShowMessage('s has not a number');
TryStrToInt
. And it requires at least two local variables. –
Landlady TryStrToInt
should be available in D7. Any reason why you accepted this solution and not the one posted by bassfriend? –
Landlady TryStrToInt
is in D7, as well as its friends TryStrToInt64
, TryStrToBool
, TryStrToFloat
, TryStrToCurr
, TryStrToDate
, TryStrToTime
and TryStrToDateTime
. Seems to me that the OP has accepted the wrong answer. –
Hattiehatton Try this function StrToIntDef()
From help
Converts a string that represents an integer (decimal or hex notation) to a number with error default.
Pascal
function StrToIntDef(const S: string; Default: Integer): Integer;
Edit
Just now checked the source of TryStrToInt()
function in Delphi 2007. If Delphi 7 dont have this function you can write like this. Its just a polished code to da-soft answer
function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
E: Integer;
begin
Val(S, Value, E);
Result := E = 0;
end;
TryStrToInt
is a lot more robust and readable. –
Landlady test
contains a valid integer number iff StrToIntDef(test,-1) = StrToIntDef(test,0)
, accompanied by the test for leading '$'
chars. I would not advocate that solution, but it is possible. –
Ethelinda TryStrToInt()
–
Mozellamozelle XE4 and newer:
for ch in s do
TCharacter.IsNumber(ch);
Don't forget:
uses System.Character
+
. I'm surprised how many answers don't think of negative numbers. –
Squarerigged In delphi 7 you can use the Val
procedure. From the help:
Unit: System
Delphi syntax: procedure Val(S; var V; var Code: Integer);
S is a string-type expression; it must be a sequence of characters that form a signed real number.
V is an integer-type or real-type variable. If V is an integer-type variable, S must form a whole number.
Code is a variable of type Integer.
If the string is invalid, the index of the offending character is stored in Code; otherwise, Code is set to zero. For a null-terminated string, the error position returned in Code is one larger than the actual zero-based index of the character in error.
use this function
function IsNumber(N : String) : Boolean;
var
I : Integer;
begin
Result := True;
if Trim(N) = '' then
Exit(False);
if (Length(Trim(N)) > 1) and (Trim(N)[1] = '0') then
Exit(False);
for I := 1 to Length(N) do
begin
if not (N[I] in ['0'..'9']) then
begin
Result := False;
Break;
end;
end;
end;
Trim()
when still checking every character? Why using Break
and not Exit
as previously? Why not setting Result := True
at the end and assume failure from the very start? It also doesn't allow negative Integers or Integers with a leading +
, which is legal and must be expected. The bad indention and post formatting fit the code quality. –
Squarerigged Trim()
is not useful at all (especially done again and again) when a check would already fail at the first character. Only the end needs true
while multiple other options all result in false
, hence setting the former as default is more logical. "You must develop the function" could have been the intro of your answer right away. Without any code. –
Squarerigged For older Delphi versions from delphi 5 help example:
uses Dialogs;
var
I, Code: Integer;
begin
{ Get text from TEdit control }
Val(Edit1.Text, I, Code);
{ Error during conversion to integer? }
if Code <> 0 then
MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0);
else
Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));
end;
In some languages decimal separators are different (for example, '.' is used in English and ',' is used in Russian). For these cases to convert string to real number the following procedure is proposed:
function TryStrToFloatMultiLang(const S : String; out Value : Extended) : Boolean;
var
dc : char;
begin
Result := false;
dc := DecimalSeparator;
DecimalSeparator := '.';
try
Result := TryStrToFloat(S, Value);
except
DecimalSeparator := ',';
Result := TryStrToFloat(S, Value);
end;
DecimalSeparator := dc;
end;
Update
As @Pep mentioned TryStrToFloat catch exceptions, but it returns boolean value. So the correct code is:
function TryStrToFloatMultiLang(const S : String; out Value : Extended) : Boolean;
var
dc : char;
begin
Result := false;
dc := DecimalSeparator;
DecimalSeparator := '.';
Result := TryStrToFloat(S, Value);
if not Result then begin
DecimalSeparator := ',';
Result := TryStrToFloat(S, Value);
end;
DecimalSeparator := dc;
end;
When you using procedure
val(s, i, iCode);
and set value xd ....
val('xd', i, iCode)
as a result we obtain: 13
standard unit Variants
function VarIsNumeric(v:Variant):Boolean
© 2022 - 2024 — McMap. All rights reserved.
if Application.MessageBox('Is this a number?'#13#10+str, MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1) = IdYes then ShowMessage(str +' IS a number!') else ShowMessage('Sorry. ''+str+'' doesn't seem to be a number...');
– Dacoitystr:='Nineteen quintillion'
, and notstr:='19000000000000000000'
... In either case, my silly solution was meant to be humorous. – Dacoity