October 2018
Intermediate to advanced
370 pages
9h 15m
English
It is also possible to cast from String to Integer or from String to Double. To do this, create a String variable and cast it by using the toInt() function. See this example:
fun main (args: Array<String>) { var stringValue: String = "125" var intValue = stringValue.toInt() println("From string to int $intValue")}
Everything is fine if the String variable contains a valid integer value, but if the String variable contains anything other than integer, Kotlin will throw a NumberCast exception. Update the following stringVariable in the previous example and verify the exception like so:
var stringValue : String = "A125"
To avoid this situation, Kotlin provides the toIntorNull function. This function will return ...
Read now
Unlock full access