1. Introduction

When programming in Kotlin, we sometimes want to check if a character is alphabetic or not. This can be useful in various situations, such as verifying user input or parsing strings.

In this tutorial, we’ll explore different functions to check if a character is alphabetic in Kotlin.

2. Check if a Character Is Alphabetic or Not

Now, let’s look at various ways we can achieve this.

2.1. Using the Character.isLetter() Function

Kotlin provides a function isLetter() on the Character class that checks if a character is a letter or not:

assertTrue('a'.isLetter())
assertTrue('w'.isLetter())
assertTrue('T'.isLetter())
assertTrue('P'.isLetter())
assertFalse('%'.isLetter())
assertFalse('4'.isLetter())
assertFalse('.'.isLetter())
assertFalse('*'.isLetter())

It’s worth noting that the isLetter() function supports Unicode characters. It returns true when the character’s category is in any of the following:

  • UPPERCASE_LETTER
  • LOWERCASE_LETTER
  • TITLECASE_LETTER
  • MODIFIER_LETTER
  • OTHER_LETTER

For example, the ‘β’ (\u03B2) character belongs to the LOWERCASE_LETTER category, so isLetter() treats it as a “letter”. But ‘Ⅷ’ (\u2167) is in the LETTER_NUMBER category, so, isLetter() returns false if we pass it to the function:

assertTrue('\u03B2'.isLetter()) // <-- Unicode char: β
assertFalse('\u2167'.isLetter()) // <-- Unicode Roman number: Ⅷ (Nl category)

2.2. Using the Character.isAlphabetic() Method

Alternatively, we can also use the Java-based Character class, which has an isAlphabetic() function, to check if a character is alphabetic or not:

assertTrue(Character.isAlphabetic('a'.code))
assertTrue(Character.isAlphabetic('w'.code))
assertTrue(Character.isAlphabetic('T'.code))
assertTrue(Character.isAlphabetic('P'.code))
assertFalse(Character.isAlphabetic('%'.code))
assertFalse(Character.isAlphabetic('4'.code))
assertFalse(Character.isAlphabetic('.'.code))
assertFalse(Character.isAlphabetic('*'.code))

Like the isLetter() function, isAlphabetic() also supports Unicode characters. It covers all categories isLetter() checks. Additionally, isAlphabetic() returns true if the character is from the LETTER_NUMBER category:

assertTrue(Character.isAlphabetic('\u03B2'.code)) //  β
assertTrue(Character.isAlphabetic('\u2167'.code)) // Ⅷ

2.3. Using Character Range Function

Another way to check if a character is alphabetic is by using the class CharRange. First, we create a range from ‘A‘ to ‘Z‘ and ‘a‘ to ‘z‘ and then check if the character falls within that range:

fun usingRangeMethod(ch: Char): Boolean {
    return ch in 'A'..'Z' || ch in 'a'..'z'
}

Now, we test this function:

assertTrue(usingRangeMethod('a'))
assertTrue(usingRangeMethod('w'))
assertTrue(usingRangeMethod('T'))
assertTrue(usingRangeMethod('P'))
assertFalse(usingRangeMethod('%'))
assertFalse(usingRangeMethod('4'))
assertFalse(usingRangeMethod('.'))
assertFalse(usingRangeMethod('*'))

As we can see, our function reports expected results when the input characters are ASCII characters. However, it doesn’t work for Unicode characters:

assertFalse(usingRangeMethod('\u03B2')) //  β
assertFalse(usingRangeMethod('\u2167')) // Ⅷ

2.4. Using Regular Expressions

Also, we can use regular expressions to check if a character is alphabetic. Basically, regular expressions are patterns used to match character combinations in strings:

fun usingRegexMethod(ch: Char): Boolean {
    return ch.toString().matches("[a-zA-Z]".toRegex())
}

Let’s verify this function works as expected:

assertTrue(usingRegexMethod('a'))
assertTrue(usingRegexMethod('w'))
assertTrue(usingRegexMethod('T'))
assertTrue(usingRegexMethod('P'))
assertFalse(usingRegexMethod('%'))
assertFalse(usingRegexMethod('4'))
assertFalse(usingRegexMethod('.'))
assertFalse(usingRegexMethod('*'))

Similarly, since the regex character class doesn’t cover Unicode characters, we cannot use this approach to determine if a Unicode character is alphabetic:

assertFalse(usingRegexMethod('\u03B2')) //  β
assertFalse(usingRegexMethod('\u2167')) // Ⅷ

2.5. Using ASCII Values

Finally, let’s use ASCII values to determine if a character is alphabetic.

The ASCII values for uppercase letters are between 65 and 90. For lowercase letters, they’re between 97 and 122. So, all we need to do is check that the ASCII value for a character is within those ranges:

fun usingAsciiValues(ch: Char): Boolean {
    return (ch.code in 65..90) || (ch.code in 97..122)
}

As usual, let’s look at some test cases:

assertTrue(usingAsciiValues('a'))
assertTrue(usingAsciiValues('w'))
assertTrue(usingAsciiValues('T'))
assertTrue(usingAsciiValues('P'))
assertFalse(usingAsciiValues('%'))
assertFalse(usingAsciiValues('4'))
assertFalse(usingAsciiValues('.'))
assertFalse(usingAsciiValues('*'))

As the ASCII table doesn’t cover all Unicode characters, the function may report wrong results for Unicode inputs:

assertFalse(usingAsciiValues('\u03B2')) //  β
assertFalse (usingAsciiValues('\u2167')) // Ⅷ

3. Conclusion

In this article, we’ve explored various techniques to determine if a character is alphabetic in Kotlin.

In our exploration, we used built-in functions like Character.isLetter() and Character.isAlphabetic() to accomplish our tasks. Notably, we also delved into regular expressions and leveraged ASCII values to achieve our goals. Moreover, we discussed these approaches in the context of Unicode support, illustrating their efficacy through practical examples.

As always, the code samples for this article can be found over on GitHub.

Comments are closed on this article!