String.ToInteger

From Xojo Documentation

Method

Returns the integer equivalent of the source string. This function is the same as the Val function but is international-savvy.

Usage

result = stringVariable.ToInteger

Part Type Description
result Integer The integer equivalent of the string passed, if an integer equivalent exists.
stringVariable String Any variable of type String.

Notes

For localized number formatting, use the ToInteger function instead. Generally, you will use Val for converting internal data and use ToInteger for converting data for input and output of user data.

See the Val function for more information.

ToInteger returns zero if string contains no numbers, except in the special case where the string begins with the string “NAN”. In this case, it returns "NAN(021)".

Numbers are converted only if they are found at the beginning of the string. Any numbers that follow a non-numeric value are ignored.

So,

 "1AA2" returns 1
 "AA2" returns 0
 "12AA54" returns 12

Sample Code

This code use the ToInteger function to return the numbers contained in a String.

Var source As String
Var result As Integer

source = "12345"
result = source.ToInteger // returns 12345

source = "54.05car45"
result = source.ToInteger // returns 54

source = "123.45"
result = source.ToInteger // returns 123

source = "123 45"
result = source.ToInteger // returns 123

source = "123,456"
result = source.ToInteger // returns 123

source = "auto"
result = source.ToInteger // returns 0

source = "&hFFF"
result = source.ToInteger // returns 4095

source = "&b1111"
result = source.ToInteger // returns 15

See Also

String.ToInt64, CStr, Integer.FromString, Format, IsNumeric, Str, Val functions.