To check if a string contains only numeric digits in Kotlin, you can use the all
function with a lambda expression to check if each character of the string is a digit.
In this example,
containsOnlyNumericDigits
that takes a string as input.all
function with a lambda expression to check if each character of the string is a digit.allDigits
will be true
, otherwise false
.fun containsOnlyNumericDigits(str: String): Boolean {
return str.all { it.isDigit() }
}
fun main() {
val str1 = "12345"
val allDigits = containsOnlyNumericDigits(str1)
println("String contains only numeric digits: $allDigits")
}
String contains only numeric digits: true
In this tutorial, we learned How to Check if String contains only Numeric Digits in Kotlin language with well detailed examples.