try_parse
Description:
Converts the string to its equivalent boolean representation
Example: String to bool, with error detection:
public int main (string[] args) {
bool result = false;
// Output: ``true``
if (bool.try_parse ("true", out result)) {
print ("%s\n", result.to_string ());
} else {
print ("Error! %s\n", result.to_string ());
}
// Output: ``Error! false``
if (bool.try_parse ("TRUE", out result)) {
print ("%s\n", result.to_string ());
} else {
print ("Error! %s\n", result.to_string ());
}
// Output: ``Error! false``
if (bool.try_parse ("1", out result)) {
print ("%s\n", result.to_string ());
} else {
print ("Error! %s\n", result.to_string ());
}
return 0;
}
valac --pkg glib-2.0 bool.try_parse.vala
Parameters:
str |
the string to convert |
result |
the location to store the value |
Returns:
true on success, or false. |