Check if a String is numeric in Kotlin
This article explores different ways to check if a String is numeric in Kotlin.
1. Using toDouble() function
The toDouble() function parses the string as a Double number and returns the result. To determine if the given string is numeric, enclose the toDouble() function call within a try-catch block, and return true if and only if the string is not parsable and no exception is thrown. The following program demonstrates its usage for numerous strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
fun isNumeric(s: String): Boolean { return try { s.toDouble() true } catch (e: NumberFormatException) { false } } fun main() { println(isNumeric("AB")) // false println(isNumeric("AB12")) // false println(isNumeric("FF")) // false println(isNumeric("0xFF")) // false println(isNumeric(" 100")) // true println(isNumeric("09")) // true println(isNumeric("100")) // true println(isNumeric("-100")) // true println(isNumeric("3.14")) // true println(isNumeric(Int.MAX_VALUE.toString())) // true println(isNumeric(Long.MAX_VALUE.toString())) // true println(isNumeric(Double.MAX_VALUE.toString())) // true } |
To parse a String as an Int, use the toInt() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
fun isInteger(s: String): Boolean { return try { s.toInt() true } catch (e: NumberFormatException) { false } } fun main() { println(isInteger("AB")) // false println(isInteger("AB12")) // false println(isInteger("FF")) // false println(isInteger("0xFF")) // false println(isInteger(" 100")) // false println(isInteger("3.14")) // false println(isInteger(Long.MAX_VALUE.toString())) // false println(isInteger(Double.MAX_VALUE.toString())) // false println(isInteger("09")) // true println(isInteger("100")) // true println(isInteger("-100")) // true println(isInteger(Int.MAX_VALUE.toString())) // true } |
2. Using NumberFormat.parse() function
Another option is to use the NumberFormat.parse() function to parse a string. If the object can’t be parsed, it does not throw an exception, but the index remains unchanged. A typical implementation of this function would look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.text.NumberFormat import java.text.ParsePosition fun isNumeric(s: String): Boolean { val pos = ParsePosition(0) NumberFormat.getInstance().parse(s, pos) return s.length == pos.index } fun main() { println(isNumeric("AB")) // false println(isNumeric("AB12")) // false println(isNumeric("FF")) // false println(isNumeric("0xFF")) // false println(isNumeric(" 100")) // false println(isNumeric("09")) // true println(isNumeric("100")) // true println(isNumeric("-100")) // true println(isNumeric("3.14")) // true println(isNumeric(Int.MAX_VALUE.toString())) // true println(isNumeric(Long.MAX_VALUE.toString())) // true println(isNumeric(Double.MAX_VALUE.toString())) // true } |
3. Using allMatch() function
To check if the string is just a sequence of digits, use the allMatch() function. The following solution demonstrates this using the Character.isDigit() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun isNumeric(s: String): Boolean { return s.chars().allMatch { Character.isDigit(it) } } fun main() { println(isNumeric("AB")) // false println(isNumeric("AB12")) // false println(isNumeric("-100")) // false println(isNumeric("3.14")) // false println(isNumeric(Double.MAX_VALUE.toString())) // false println(isNumeric("FF")) // false println(isNumeric("0xFF")) // false println(isNumeric(" 100")) // false println(isNumeric("09")) // true println(isNumeric("100")) // true println(isNumeric(Int.MAX_VALUE.toString())) // true println(isNumeric(Long.MAX_VALUE.toString())) // true } |
That’s all about checking if a String is numeric in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)