Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 4, 2024
In this tutorial, we’ll show how to check if a Character is alphanumeric using only the Scala standard library.
An alphanumeric character is either a letter (from A to Z, regardless of case) or a digit (from 0 to 9). In other words, an alphanumeric character is a character that is part of the alphabet (letters) or part of the numeric system (digits).
In this first solution, we’ll add all alphanumeric characters to a Set and compare if the Character exists in the Set. Let’s start by adding all valid alphanumeric characters:
scala> val alphanumericChars = (('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9')).toSet
alphanumericChars: scala.collection.immutable.Set[Char] = Set(E, e, X, s, x, 8, 4, n, 9, N, j, y, T, Y, t, J, u, U, f, F, A, a, 5, m, M, I, i, v, G, 6, 1, V, q, Q, L, b, g, B, l, P, p, 0, 2, C, H, c, W, h, 7, r, K, w, R, 3, k, O, D, Z, o, z, S, d)
We can now define a method to check if a given Character is alphanumeric using the Set we just created:
scala> def isAlphanumeric(c: Char) = alphanumericChars.contains(c)
isAlphanumeric: (c: Char)Boolean
And if we now test, we see it works as it should:
scala> isAlphanumeric('3')
res0: Boolean = true
scala> isAlphanumeric('a')
res1: Boolean = true
scala> isAlphanumeric('D')
res2: Boolean = true
scala> isAlphanumeric('?')
res3: Boolean = false
scala> isAlphanumeric('-')
res4: Boolean = false
The number and letters we tested returned true, while the interrogation mark and dash returned false.
Another very common solution is to use regular expressions, also known as regex. We start by defining the proper regex:
val nonAlphanumericRegex = new Regex("^[^a-zA-Z0-9]+$")
Let’s start by understanding the regex we just created:
Putting it all together, the regex “^[^a-zA-Z0-9]+$” matches strings that consist entirely of non-alphanumeric characters.
Now we can check if a String contains an alphanumeric Character:
scala> nonAlphanumericRegex.findFirstIn("?")
res0: Option[String] = Some(?)
scala> nonAlphanumericRegex.findFirstIn("3")
res1: Option[String] = None
If the String contains a non-alphanumeric, then the Regex.findFirstIn() method will return a non-empty Option. Otherwise, it will just return None.
Scala also provides a straightforward method through the RichChar class:
scala> "?".forall(_.isLetterOrDigit)
res20: Boolean = false
scala> "abc345".forall(_.isLetterOrDigit)
res21: Boolean = true
scala> "abc-345".forall(_.isLetterOrDigit)
res22: Boolean = false
Here, we used the RichChar.isLetterOrDigit() method to check that all characters are alphanumeric.
In this article, we learned how to check if a Character or String contains only alphanumeric characters. We started by using a simple Set containing all digits and letters, then moved to the powerful regex engine in Scala. Finally, we looked at the very useful RichChar class provided by the Scala standard library.