System.SysUtils.TryStrToInt

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

function TryStrToInt(const S: string; out Value: Integer): Boolean;

C++

extern DELPHI_PACKAGE bool __fastcall TryStrToInt(const System::UnicodeString S, /* out */ int &Value)/* overload */;

Properties

Type Visibility Source Unit Parent
function public
System.SysUtils.pas
System.SysUtils.hpp
System.SysUtils System.SysUtils

Description

Converts a string that represents an integer (decimal or hex notation) into a number with Boolean success code.

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.

TryStrToInt supports strings in the following hexadecimal notations:

  • Delphi: $1234 and 0x1234.
  • C++: 0x1234.

To accept decimal but not hexadecimal values in the input string, you may use code like this:

Delphi:

function TryDecimalStrToInt( const S: string; out Value: Integer): Boolean;
begin
  result := (pos('$',S)=0) and ((pos('x',S)=0)) and TryStrToInt(S,Value);
end;

C++:

bool __fastcall TForm1::TryDecimalStrToInt(const System::UnicodeString S, int &Value)
{
  bool result;
  result= (Pos('x',S)==0) && TryStrToInt(S,Value);
  return result;
}
Note: In Delphi, you can also use the C++ hexadecimal values notation, and use prefixes 0x or x indistinctly.

See Also