In this article, we’ll explore different alternatives to check if a string contains only letters in C#. We will learn the characteristics of each approach, discuss the benefits of each one, and choose the one that best fits each situation.
So, let’s dive into the topic.
The Encoding Matters
The Encodings define the codes used for character representation in digital systems. Each letter, number, or symbol has a unique identification. But, there is more than one Encoding definition and we need to take care of that when manipulating texts. In this article, we will mention the two most used formats, ASCII and UTF-8.
The ASCII (American Standard Code for Information Interchange) representation uses 7 bits of one byte to create a dictionary of symbols, with a total of 128 different characters. Differently, the UTF-8 (Unicode Transformation Format 8) encoding uses from 1 up to 4 bytes per character depending on the represented symbol. As a result, we have many more available symbols such as Greek, Arabic, Armenian, Hebrew, and other alphabets.
From the documentation, Microsoft uses different ranges for ASCII letters and UTF-8 letters, which can lead us to different results when checking if a text is only a letter string.
Use LINQ to Check if a String Contains Only Letters in C#
The Language-Integrated Query (LINQ) is a powerful set of functionalities for data access. This tool, certainly, simplifies how we interact with our data:
public static bool IsOnlyLetters(string text) { return text.All(char.IsLetter); }
In this example, the LINQ interprets the text as an Enumerable<Char>
. Thus, we use the char.IsLetter
as a predicate for the LINQ method All()
. Like magic, the checking will be applied to every string element. In the end, our method returns true if the string contains only letters. Otherwise, it returns false.
But, the IsLetter
predicate assumes that we are working with the UTF-8 encoding. It’s not always true and we have an alternative method for ASCII-encoded texts:
public static bool IsOnlyAsciiLetters(string text) { return text.All(char.IsAsciiLetter); }
The solution is simple since the char
type already has a valid method for checking ASCII texts. In this scenario, we just use the predicate char.IsAsciiLetter
.
Use Regex to Check if a String Contains Only Letters in C#
The regular expressions (Regex) quickly come to mind when we are working with texts. With Regex, we can create flexible patterns as complex as we want. And, next, we can use it to match character combinations in a string.
The static method IsMatch is a one-liner operation that keeps the code simple and straightforward. Specifically, its first parameter is the text that is searched and, the second, is the pattern expected to match:
static bool IsOnlyLetters_Method2(string text) { return Regex.IsMatch(text, @"^[\p{L}]+$"); }
Since IsMatch
returns true if the string matches the specified pattern, we directly forward its result.
Again, this method matches an enormous number of characters. But, if we want to check a string contains only letters in an ASCII text, the solution is also very simple. We just need to vary our search pattern to one like these, and they give us the same result:
[a-zA-Z]
[\x41-\x5A\x61-\x7A]
Native Implementation to Check if a String Contains Only Letters in C#
Before we dive into the examples, let’s define what we call “native”. In this case, we refer to “native” as a solution that doesn’t depend on external libraries. In other words, it’s a solution that we can achieve using only native types and loops. Independent of the solution, the native implementation requires iterating over all the elements in the string and checking if it’s a letter or not. To achieve this goal, we can combine different loops and checkings, but, in this article, we’ll highlight two implementations.
Pattern Matching
The pattern-matching technique is particularly interesting because of its syntax. It’s concise, expressive, and easily readable:
public static bool IsOnlyAsciiLettersByPatternMatching(string text) { foreach (var item in text) { if (item is >= 'A' and <= 'Z' or >= 'a' and <= 'z') continue; else return false; } return true; }
Here, we use pattern matching to check the range where the character is placed. The method returns true if, at least, one character is located out of the ranges considered letters.
Using Switch-Case Instruction
The switch-case is another instruction that can be applied to check if a ‘char’ is a letter or not. Its structure makes it very easy to define the valid interval for letters. Moreover, we can easily customize our set of strings if needed. This approach keeps a clear and simple implementation:
public static bool IsOnlyAsciiLettersBySwitchCase(string text) { foreach (var item in text) { switch (item) { case >= 'A' and <= 'Z': case >= 'a' and <= 'z': continue; default: return false; } } return true; }
In this example, we create the custom interval to consider Latin and Greek alphabet letters. Each case
represents a valid letter interval. This way, we return false only if, at least, one character in the text is out of the defined range.
The Best Alternative While Checking if a String Contains Only Letters
There is no silver bullet in this case. When selecting the best option for our application, we need to care about critical characteristics, like execution time, external dependencies, and framework version, for example.Â
Currently, it’s hard to find an application that can’t use the LINQ library. Its wide usage and simplicity make this library one of the choices to check if it’s an only-letter string. On the other hand, for those cases when time is not an essential requirement, the Regex arises as an option.
Do we have restrictions to use both of the previous implementations? Ok! It’s not a problem. We still can adopt any of the available native implementations for checking if a string is only letters. They are independent of external libraries and are fast as the LINQ approach.
Conclusion
In this article, we discussed how to check if a string is only composed of letters. Firstly, we explained the importance of the used encoding when handling texts. Next, we presented three alternatives on how to implement this checking and, then, we finished arguing about how to select the best implementation format.