How to check if a string contains only digits in Kotlin

1 Answer

0 votes
fun isDigitsOnly(s: String): Boolean {
    return s.matches(Regex("\\d+"))
}

fun main() {
    var s = "748091"
    println(isDigitsOnly(s))
    
    s = "837R6"
    println(isDigitsOnly(s))
}
 

 
/*
run:

true
false
 
*/

 



answered Dec 22, 2024 by avibootz
...