Unit 'sysutils' Package
[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] [#rtl]

StrToInt

Convert a string to an integer value.

Declaration

Source position: sysstrh.inc line 135

function StrToInt(

  const s: string

):LongInt;

Description

StrToInt will convert the string S to a 32-bit signed integer. If the string contains invalid characters or has an invalid format, then an EConvertError is raised.

To be successfully converted, a string can contain a combination of numerical characters, possibly preceded by a minus sign (-). Spaces and tab characters are only allowed at the start of the string. Internally, Val is used, the rules of Val apply.

The string S can contain a number in decimal, hexadecimal, binary or octal format, as described in the language reference. For enumerated values, the string must be the name of the enumerated value. The name is searched case insensitively.

For hexadecimal values, the prefix '0x' or 'x' (case insensitive) may be used as well in addition to the customary number notations described in the language reference.

Errors

In case of error, an EConvertError is raised.

See also

Val

  

Calculate numerical/enumerated value of a string.

IntToStr

  

Convert an integer value to a decimal string.

StrToUint

  

Convert a string to unsigned integer value.

StrToInt64

  

Convert a string to an Int64 value.

StrToIntDef

  

Convert a string to an integer value, with a default value.

EConvertError

  

Conversion error.

Example

Program Example82;

{$mode objfpc}

{ This program demonstrates the StrToInt function }

Uses sysutils;

Begin
  Writeln (StrToInt('1234'));
  Writeln (StrToInt('-1234'));
  Writeln (StrToInt('0'));
  Try
    Writeln (StrToInt('12345678901234567890'));
  except
    On E : EConvertError do
      Writeln ('Invalid number encountered');
  end;
End.

Documentation generated on: Dec 21 2024