fun Char.digitToIntOrNull(): Int?
Returns the numeric value of the decimal digit that this Char represents, or null if this Char is not a valid decimal digit.
A Char is considered to represent a decimal digit if isDigit is true for the Char. In this case, the Unicode decimal digit value of the character is returned.
import java.util.*
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
println('5'.digitToIntOrNull()) // 5
println('3'.digitToIntOrNull(radix = 8)) // 3
println('A'.digitToIntOrNull(radix = 16)) // 10
println('K'.digitToIntOrNull(radix = 36)) // 20
// radix argument should be in 2..36
// '0'.digitToIntOrNull(radix = 1) // will fail
// '1'.digitToIntOrNull(radix = 100) // will fail
// only 0 and 1 digits are valid for binary numbers
println('5'.digitToIntOrNull(radix = 2)) // null
// radix = 10 is used by default
println('A'.digitToIntOrNull()) // null
// symbol '+' is not a digit in any radix
println('+'.digitToIntOrNull()) // null
// Only Latin letters are valid for digits greater than 9.
println('β'.digitToIntOrNull(radix = 36)) // null
//sampleEnd
}fun Char.digitToIntOrNull(radix: Int): Int?
Returns the numeric value of the digit that this Char represents in the specified radix, or null if this Char is not a valid digit in the specified radix. Throws an exception if the radix is not in the range 2..36.
A Char is considered to represent a digit in the specified radix if at least one of the following is true:
true for the Char and the Unicode decimal digit value of the character is less than the specified radix. In this case the decimal digit value is returned.radix + 'A'.code - 10. In this case, this.code - 'A'.code + 10 is returned.radix + 'a'.code - 10. In this case, this.code - 'a'.code + 10 is returned.radix + 0xFF21 - 10. In this case, this.code - 0xFF21 + 10 is returned.radix + 0xFF41 - 10. In this case, this.code - 0xFF41 + 10 is returned.import java.util.*
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
println('5'.digitToIntOrNull()) // 5
println('3'.digitToIntOrNull(radix = 8)) // 3
println('A'.digitToIntOrNull(radix = 16)) // 10
println('K'.digitToIntOrNull(radix = 36)) // 20
// radix argument should be in 2..36
// '0'.digitToIntOrNull(radix = 1) // will fail
// '1'.digitToIntOrNull(radix = 100) // will fail
// only 0 and 1 digits are valid for binary numbers
println('5'.digitToIntOrNull(radix = 2)) // null
// radix = 10 is used by default
println('A'.digitToIntOrNull()) // null
// symbol '+' is not a digit in any radix
println('+'.digitToIntOrNull()) // null
// Only Latin letters are valid for digits greater than 9.
println('β'.digitToIntOrNull(radix = 36)) // null
//sampleEnd
}
© 2010–2023 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/digit-to-int-or-null.html