1. Overview

In this tutorial, we’ll show how to check if a Character is alphanumeric using only the Scala standard library.

2. What Is an Alphanumeric Character

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).

3. Using a Set

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.

4. Using Regular Expression

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:

  1. ^: This anchor symbol denotes the start of the string. It indicates that the pattern must match from the beginning of the string.
  2. [^a-zA-Z0-9]: Inside square brackets, the caret ^ at the beginning indicates negation. So, [^a-zA-Z0-9] matches any character that is not a lowercase letter, uppercase letter, or digit.
  3. +: The plus sign quantifier means “one or more occurrences” of the preceding negated character class. So, [^a-zA-Z0-9]+ matches one or more occurrences of any non-alphanumeric character.
  4. $: This anchor symbol denotes the end of the string. It indicates that the pattern must match until the end of the string.

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.

5. Using RichChar

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.

6. Conclusion

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.

Notify of
Inline Feedbacks
View all comments