The `parse` method in Dart parses a string as a, possibly signed, integer literal and returns its value.
The syntax of int.parse() static-method is:
int parse(String source, { int radix, int onError(String source) })
This parse() static-method of int parse source as a, possibly signed, integer literal and return its value.
Parameter | Optional/Required | Description |
---|---|---|
source | required | The string to parse as an integer. |
radix | optional | The radix of the parsed integer. If not provided, it defaults to 10. |
onError | optional | A function that handles errors encountered during parsing. If not provided, an exception is thrown. |
int.parse() returns value of type int
.
In this example,
source
containing the integer 123
.parse
method to parse the string as an integer.void main() {
String source = '123';
int value = int.parse(source);
print('Parsed integer value: $value');
}
Parsed integer value: 123
In this example,
source
containing the integer -456
.parse
method to parse the string as an integer.void main() {
String source = '-456';
int value = int.parse(source);
print('Parsed integer value: $value');
}
Parsed integer value: -456
In this example,
source
containing the hexadecimal integer FF
.parse
method with a radix of 16
to parse the string as an integer.void main() {
String source = 'FF';
int value = int.parse(source, radix: 16);
print('Parsed integer value: $value');
}
Parsed integer value: 255
In this Dart tutorial, we learned about parse() static-method of int: the syntax and few working examples with output and detailed explanation for each example.