Home  Contents

string.val

String Core4 Lua Commands

SYNOPSIS

result = string.val(s [, i [, j]])

DESCRIPTION

Returns the numerical value of the substring s[i], s[i+1], ···, s[j].
The default value for i is 1; the default value for j is the number of characters in s.

RETURN VALUE

On success, a numerical value. If the substring doesn't decode to a number, returns nil.

NOTES

Generic Lua already supports turning strings into numbers natively. The string.val() described here has two advantages over the built in type coercion:

  • You can specify a substring easily without creating more garbage.
  • The application will not terminate if the data cannot be converted to a number. You just get nil.

EXAMPLE

print(string.val("XX42YYYY", 3, 4)
42