Converting a text string to an integer with an optional character, considering the default value.

 

Syntax

function StrToIntDef(st: string; iDefaultValue: integer): integer;

 

Parameters and return values

Parameter

Type

Value

st

string

source string;

iDefaultValue

integer

default integer value that returns if the conversion fails.

 

Function result

Integer that corresponds to the transmitted string or the default value.

 

Example


var
  A, B, C, D, E, F: integer;
begin
  A := 32;
  B := StrToIntDef('100', 0);    // string '100' converts to int 100
  C := StrToIntDef('  -12', 0);  // initial spaces are ignored
  D := StrToIntDef('$1E', 0);    // hex values started from '$'
  E := StrToIntDef('-0x1E', 0);  // ... or from '0x'
  F := A + B + C + D + E;  // add up all of these integers
  mLogScript('A : ' + IntToStr(A), '');
  mLogScript('B : ' + IntToStr(B), '');
  mLogScript('C : ' + IntToStr(C), '');
  mLogScript('D : ' + IntToStr(D), '');
  mLogScript('E : ' + IntToStr(E), '');
  mLogScript('F : ' + IntToStr(F), '');
end.


Script work result

[18:36:50] (Log "StrToIntDef"): A : 32

[18:36:50] (Log "StrToIntDef"): B : 100

[18:36:50] (Log "StrToIntDef"): C : -12

[18:36:50] (Log "StrToIntDef"): D : 30

[18:36:50] (Log "StrToIntDef"): E : -30

[18:36:50] (Log "StrToIntDef"): F : 120

 

See also

IntToStr

mLogScript