Convert String to Integer in Kotlin

To convert a numeric string to an Int in Kotlin, call String.toInt(). For input that may be invalid, use String.toIntOrNull() so that the conversion returns null instead of throwing an exception. On Kotlin/JVM, Integer.parseInt() is also available through Java interoperability.

For example, "241".toInt() returns the integer value 241. The conversion parses the whole string; it does not extract digits from text such as "Order 241". If the number is embedded in other text, isolate the numeric part first and then convert it.

String-to-Int conversion is commonly needed for values read from user input, text files, configuration values, command-line arguments, form fields, or other sources where numbers initially arrive as strings.

Kotlin String to Int syntax with toInt() and Integer.parseInt()

The syntax of String.toInt() is given below.

</>
Copy
 String.toInt()

String.toInt() returns a Kotlin Int when the string contains a valid integer representation. If the string is not a valid Int, or the numeric value is outside the Int range, the conversion throws java.lang.NumberFormatException.

The syntax of Integer.parseInt() is given below.

</>
Copy
 Integer.parseInt(String)

On Kotlin/JVM, Integer.parseInt() takes the string as an argument and returns an integer when parsing succeeds. Invalid input throws java.lang.NumberFormatException, just as with String.toInt(). In Kotlin code, toInt() is usually the clearer choice because it is a Kotlin standard-library conversion function.

The Kotlin standard library reference for toInt() is available at kotlinlang.org.

Kotlin String to Int conversion examples

1. Convert String to Integer using String.toInt()

Here, the string "241" contains a valid decimal integer. Calling toInt() converts it to a Kotlin Int and stores the result in num.

example.kt

</>
Copy
/**
 * Kotlin - Convert Sting to Integer
 */
fun main(args: Array<String>) {
    //a string
    val str = "241"
    //convert string to integer
    val num = str.toInt()

    print(num+10)
}

The expression num + 10 performs integer addition. Since num is 241, the program prints 251.

Output

251

2. Convert String to Integer using Integer.parseInt()

This Kotlin/JVM example passes the string "241" to Integer.parseInt() and stores the returned integer in num.

example.kt

</>
Copy
/**
 * Kotlin - Convert Sting to Integer
 */
fun main(args: Array<String>) {
    //a string
    val str = "241"
    //convert string to integer
    val num = Integer.parseInt(str)

    print(num+10)
}

As in the previous example, adding 10 to the parsed value produces 251.

Output

251

3. Invalid Kotlin String to Int conversion and NumberFormatException

The string "241a" is not a valid integer representation because it contains the letter a. Calling toInt() on this value throws NumberFormatException.

example.kt

</>
Copy
/**
 * Kotlin - Convert Sting to Integer
 */
fun main(args: Array<String>) {
    //a string
    val str = "241a"
    //convert string to integer
    val num = str.toInt()

    print(num+10)
}

Running the program produces an exception rather than an integer result.

Output

Exception in thread "main" java.lang.NumberFormatException: For input string: "241a"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.parseInt(Integer.java:770)
	at KotlinStringToIntegerKt.main(KotlinStringToInteger.kt:8)

The exception occurs because toInt() requires the complete string to represent an integer. Use toIntOrNull() when invalid input should be handled without throwing this exception.

Handle invalid Kotlin String input with toIntOrNull()

Use toIntOrNull() when the string may come from a user, file, network response, or another source that can contain invalid data. It returns an Int?: a valid integer when parsing succeeds, or null when the string cannot be converted to an Int.

</>
Copy
fun main() {
    val text = "241a"
    val number = text.toIntOrNull()

    println(number)
}

Output

null

If your program needs a fallback value, combine toIntOrNull() with Kotlin’s Elvis operator ?:. The expression on the right is used only when the conversion result is null.

</>
Copy
fun main() {
    val text = "not-a-number"
    val number = text.toIntOrNull() ?: 0

    println(number)
}

Output

0

Convert binary and hexadecimal Strings to Int with a radix

Kotlin also provides toInt(radix) when the string represents a number in a base other than decimal. For example, use radix 2 for binary and radix 16 for hexadecimal.

</>
Copy
fun main() {
    val binary = "1010"
    val hexadecimal = "FF"

    println(binary.toInt(2))
    println(hexadecimal.toInt(16))
}

Output

10
255

If radix-based input may be invalid, use toIntOrNull(radix) instead.

Whitespace, signs, and Int range during String conversion

  • Leading or trailing spaces: clean input first when whitespace is possible, for example text.trim().toIntOrNull().
  • Positive and negative signs: strings such as "42", "+42", and "-42" can represent decimal integers.
  • Decimal points: a value such as "42.5" is not an Int representation. Parse it as a floating-point type when that is the intended data.
  • Int range: Kotlin Int values range from -2147483648 to 2147483647. A numeric string outside this range cannot be converted with toInt().
  • Embedded text: "id=42" is not directly convertible with toInt(); first obtain the numeric substring.

Choosing toInt() or toIntOrNull() for Kotlin String conversion

Use toInt() when the string is expected to contain a valid integer and invalid input should be treated as an error. Use toIntOrNull() when invalid input is possible and you want to handle it without a NumberFormatException. For Kotlin/JVM code, Integer.parseInt() also works, but String.toInt() is generally more idiomatic Kotlin.

This Kotlin Tutorial covered String.toInt(), Integer.parseInt(), the safer toIntOrNull() alternative, radix conversion, and common invalid-input cases.