parse


Description:

[ CCode ( cname = "atoi" ) ]
public static int parse (string str)

Converts the string to its equivalent int representation

Function discards any whitespace characters until first non-whitespace character is found. Then it takes as many characters as possible to form a valid integer number representation and converts them to integer value. The valid integer value consists of the following parts:

  • (optional) plus or minus sign
  • numeric digits

Example: String to int:

public static int main (string[] args) {
// Output: ``10``
print ("%d\n", int.parse ("10"));

// Output: ``10``
print ("%d\n", int.parse ("10"));

// Output: ``10``
print ("%d\n", int.parse ("10xx"));

// Output: ``0``
print ("%d\n", int.parse ("A"));

// Output: ``0``
print ("%d\n", int.parse ("a"));

// Output: ``0``
print ("%d\n", int.parse ("x11"));

// Output: ``2147483647``
print ("%d\n", int.parse ("999999999999999999999999999999999"));

return 0;
}

valac --pkg glib-2.0 int.parse.vala

Parameters:

str

the string to convert

Returns:

Integer value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0 is returned.

See also:

int.try_parse