In the tutorial, JavaSampleApproach will show you how to convert Kotlin String to Int.
Related posts:
– Kotlin Convert String to Long
Working environment:
– Java 8
– Kotlin 1.1.61
Contents
Kotlin toInt() method
String.toInt(): Int
– use method signature: public inline fun String.toInt(): Int
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javasampleapproach.string2int fun main(args : Array<String>) { // use method: // -> public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this) val number: Int = "123".toInt(); println(number) // 123 // if the string is not a valid representation of a number // -> throw NumberFormatException try{ "12w".toInt(); }catch(e: NumberFormatException){ println(e.printStackTrace()) // -> print on console: /* java.lang.NumberFormatException: For input string: "12w" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:12) */ } } |
– Strig.toInt()
will throw a NumberFormatException if the string is not a valid representation of a number.
– String.toInt()
just uses Integer.parseInt
of Java for converting
-> detail: public inline fun String.toInt(): Int = java.lang.Integer.parseInt(this)
String.toInt(radix: Int): Int
If we want to work with radix, we can use another method signature toInt(radix: Int)
-> detail: public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javasampleapproach.string2int fun main(args : Array<String>) { // use method: // -> public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix)) val numberWithRadix2: Int = "101".toInt(2) println("numberWithRadix2 = $numberWithRadix2") // numberWithRadix2 = 5 // if the string is not a valid representation of a number // -> throw NumberFormatException try{ "123".toInt(2); }catch(e: NumberFormatException){ println(e.printStackTrace()) // -> print on console: /* java.lang.NumberFormatException: For input string: "123" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:12) */ } // if radix is not a valid radix // -> throw IllegalArgumentException try{ "101".toInt(-1) }catch(e: IllegalArgumentException){ println(e.printStackTrace()) // -> print on console: /* java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36 at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155) at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:28) */ } } |
Exception:
String.toInt(radix: Int)
will throw a NumberFormatException if the string is not a valid representation of a number.String.toInt(radix: Int)
will throw a IllegalArgumentException if the radix is not a valid radix.
– String.toInt(radix: Int)
just uses Integer.parseInt(this, checkRadix(radix))
of Java for converting,
-> detail: public inline fun String.toInt(radix: Int): Int = java.lang.Integer.parseInt(this, checkRadix(radix))
Kotlin toIntOrNull() method
String.toIntOrNull(): Int?
Method signature: public fun String.toIntOrNull(): Int?
-> Parses the string to an Int number and returns a number or null in case the string is not a valid representation of a number.
It does not throw any exception.
1 2 3 4 5 6 7 8 9 10 11 |
package com.javasampleapproach.string2int fun main(args : Array<String>) { // use method: // -> public fun String.toIntOrNull(): Int? val number: Int? = "123".toIntOrNull() println("number = $number") // number = 123 val a: Int? = "12w".toIntOrNull() println("a = $a") // a = null } |
String.toIntOrNull(radix: Int): Int?
Method signature: public fun String.toIntOrNull(radix: Int): Int?
-> Parses the string to an Int number and returns null in-case the string is not a valid representation of a number.
And IllegalArgumentException will be thrown when radix is not a valid radix for string to number conversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javasampleapproach.string2int fun main(args : Array<String>) { // use method: // -> public fun String.toIntOrNull(): Int? val number: Int? = "101".toIntOrNull(2) println("number = $number") // number = 5 val a: Int? = "12w".toIntOrNull(2) println("a = $a") // a = null val b: Int? = "192".toIntOrNull(8) println("b = $b") // b = null try{ "101".toIntOrNull(-1) }catch(e: IllegalArgumentException){ println(e.printStackTrace()) // -> print on console /* java.lang.IllegalArgumentException: radix -1 was not in valid range 2..36 at kotlin.text.CharsKt__CharJVMKt.checkRadix(CharJVM.kt:155) at kotlin.text.StringsKt__StringNumberConversionsKt.toIntOrNull(StringNumberConversions.kt:182) at com.javasampleapproach.string2int.ConvertString2IntKt.main(ConvertString2Int.kt:16) */ } } |
Last updated on April 9, 2018.