Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Kotlin

Idiom #137 Check if string contains only digits

Set the boolean b to true if the string s contains only characters in the range '0'..'9', false otherwise.

fun String?.isOnlyDigits() = !this.isNullOrEmpty() && this.all { Character.isDigit(it) }

We can check all characters by string's all method, without any regexp
val regex = Regex("[0-9]*")
val b = regex.matches(s)
B := (for all Char of S => Char in '0' .. '9');

New implementation...
< >
programming-idioms.org