1. Overview

In this tutorial, we’ll look at some common ways to check if a String is numeric. First, we’ll talk about parsing methods and using regular expressions.

Finally, we’ll look at another method that only detects positive integers.

2. By Parsing to Double or Int

One way to check if a string is numeric is to parse it as a Double or Int (or other built-in numeric types). In case this parsing attempt doesn’t return null, we can safely assume that a String is a number:

fun isNumericToX(toCheck: String): Boolean {
    return toCheck.toDoubleOrNull() != null
}

This method can handle Double and all the numeric types like Int, Float, and so on:

assertTrue { isNumeric("11") }
assertTrue { isNumeric("-11") }
assertTrue { isNumeric("011") }
assertTrue { isNumeric("11.0F") }
assertTrue { isNumeric("11.0D") }
assertTrue { isNumeric("11.234") }
assertTrue { isNumeric("11.234e56") }
assertTrue { isNumeric("     123      ") }

This method is quite versatile and can handle all numeric types. However, we can also perform checks for the other numeric types. We can do so by replacing toDoubleOrNull with other conversion methods:

  1. toIntOrNull()
  2. toLongOrNull()
  3. toFloatOrNull()
  4. toShortOrNull()
  5. toByteOrNull()

Notably, we can call each of these conversion methods on all numeric types in Kotlin.

3. Using Regular Expressions

We can also use regular expressions to check if a String is numeric:

fun isNumeric(toCheck: String): Boolean {
    val regex = "-?[0-9]+(\\.[0-9]+)?".toRegex()
    return toCheck.matches(regex)
}

Here, we use a regex to check if the String is a number. The regex is structured to match a decimal number.

Let’s look at it closely:

  • -? – We check if the number has zero or one minus (“-“) symbol at the start.
  • [0-9] – We check if there are one or more digits in a String. This fails if there are no numbers. Notably, this only matches 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. However, if we want to look for other Unicode numerals, we can use “\d” instead of “[0-9]”.
  • (\\.[0-9]+)? – We check if there’s a decimal (“.”) symbol; if so, it must be followed by at least one digit.

4. Using isDigit() and all()

Another way to check if a String contains only digits is to use a combination of all and isDigit methods:

fun isNumeric(toCheck: String): Boolean {
    return toCheck.all { char -> char.isDigit() }
}

Here, we check if each character of a String is a numeric digit. The all method returns true if all characters return true when passed to the isDigit method.

This also means that this method doesn’t work for negative or floating-point numbers.

5. Conclusion

We looked at various ways to check if a string is numeric. First, we used the **various parsing functions that Kotlin has to offer (toDoubleOrNull(), toIntOrNull(), and others)**. This method seems the most versatile and covers the most use cases.

Later, we also looked at other options that cover relatively lesser use cases. These methods used regular expressions and Kotlin factory functions to check numeric Strings.

As always, the code samples are available over on GitHub.