kotlin

String to Int conversion in Kotlin

If you are using Strings in Kotlin and want to convert it to an Int type value then you can use the methods explained in this post. We will be using toInt(), toIntOrNull() and parseInt() methods.

fun main() {
    // define a String
    val myStr: String = "100"
    
    // convert to int
    val num: Int = myStr.toInt()
    
    // print the result
    println(num)
}

Output

100

Sometimes, you have numbers data in String format that needs to be calculated and to apply mathematics operations on it, you need to use the Integer version of those String. Kotlin provides some methods that can be used to convert a String to Integer values. We are explaining them one by one here.

Convert String to Int using toInt() function

The String.toInt() is the most popular method that is used to convert a String to an Integer type value in Kotlin. It is used with the String value and returns the Integer type value.

Syntax

String.toInt()

Code example

fun main() {
    // define a String
    val strVal: String = "40"
    
    // convert to int
    val intVal: Int = strVal.toInt()
    
    println("Sum of two int values: " + (intVal + intVal))
    
    println("Sum to two String values: " + (strVal + strVal))
}

Output

Sum of two int values: 80
Sum to two String values: 4040

What happens if the string value is not in number format

fun main() {
    // define a String
    val strVal: String = "40c"
    
    // convert to int
    val intVal: Int = strVal.toInt()
}

The above code will throw an error because the String value that we want to convert to an integer type value is not a number value. The error is shown below.

Exception in thread "main" java.lang.NumberFormatException: For input string: "40c"
 at java.lang.NumberFormatException.forInputString (:-1) 
 at java.lang.Integer.parseInt (:-1) 
 at java.lang.Integer.parseInt (:-1) 

To handle the above error we can use exception handling in Kotlin.

fun main() {
    try {
       // define a String
        val strVal: String = "40c"

        // convert to int
        val intVal: Int = strVal.toInt()
        
    } catch (err: NumberFormatException) {
        println("Not a valid value")
    }
}

Output

Not a valid value

Convert String to Int using parseInt() function

As an alternative of String.toInt(), we can also use Integer.parseInt() function to convert a String to Integer type value. We just need to pass our string value to Integer.parseInt() function as a parameter and it will return the integer type value of the string.

Syntax

Integer.parseInt(string_value)

Code example

// define a String
val strVal: String = "40"

// convert to int using Integer.parseInt()
val intVal: Int = Integer.parseInt(strVal)

println(intVal)

Output

40

Use toIntOrNull() function to convert String value to Int

We can also use String.toIntOrNull() function to convert a String value to Integer type value. The function will return an integer value if the string value is valid and return null if the string value is not valid.

val strVal = "800"

val intVal = strVal.toIntOrNull()

if (intVal != null) {
    println(intVal)
} else {
    // value is not valid
}

Output

800
Was this helpful?