To convert a String to an Integer in Kotlin, you can use the toInt()
method of the String class.
In this example,
str
with a string value of "123"
.toInt()
method of the string object str
to convert it to an integer and store the result in the variable number
.toInt()
method parses the string and returns the equivalent integer value.number
to standard output.fun main() {
val str = "123"
val number = str.toInt()
println("Integer value: $number")
}
Integer value: 123
In this example,
str
with a string value of "987"
.toInt()
method of the string object str
to convert it to an integer and store the result in the variable number
.toInt()
method parses the string and returns the equivalent integer value.number
to standard output.fun main() {
val str = "987"
val number = str.toInt()
println("Integer value: $number")
}
Integer value: 987
In this tutorial, we learned How to Convert String to Integer in Kotlin language with well detailed examples.