Check if a string contains only letters in C#
This article illustrates the different techniques to check if a string contains only letters in C#.
1. Using String.All() method
To determine if a string contains only letters in C#, you can use Enumerable.All() method by LINQ. It returns true if all elements of a sequence satisfy a condition, false otherwise. To check for only letters, pass Char.IsLetter to the All() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { string s = "Hello"; bool isAlpha = s.All(Char.IsLetter); Console.WriteLine(isAlpha); // True } } |
It should be noted that the Char.IsLetter method determines whether the char is a Unicode letter. To restrict checking for only ASCII alphabets, do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { string s = "Hello"; bool isAlpha = s.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); Console.WriteLine(isAlpha); // True } } |
To check for Unicode letters with only space allowed, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { string s = "Hello World"; bool isAlpha = s.All(c => Char.IsLetter(c) || c == ' '); Console.WriteLine(isAlpha); // True } } |
Finally, if you need to check for either Unicode letters or digits, pass the Char.IsLetterOrDigit to the All() method. To check for only numbers, you can use the Char.IsDigit method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; public class Example { public static void Main() { string s = "hello123"; bool isAlpha = s.All(Char.IsLetterOrDigit); Console.WriteLine(isAlpha); // True } } |
2. Using Regex.IsMatch() method
You can also use regular expressions to identify any non-alpha characters in a string. To check only for ASCII letters, you can use regex ^[a-zA-Z]+$. To check for only ASCII letters and numbers, you can use regex ^[a-zA-Z0-9]+$. Similarly, to check for ASCII letters, numbers, and underscore, use regex ^[a-zA-Z0-9_]+$.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string s = "Hello"; bool isAlpha = Regex.IsMatch(s, @"^[a-zA-Z]+$"); Console.WriteLine(isAlpha); // True } } |
To check only for Unicode letters, you can use the pattern ^[\p{L}]+$. To check for only Unicode letters and numbers, you can use the pattern ^[\p{L}\p{N}]+$. Similarly, to check for Unicode letters, numbers, and underscore, use the pattern ^[\w]+$.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string s = "Hello"; bool isAlpha = Regex.IsMatch(s, @"^[\p{L}]+$"); Console.WriteLine(isAlpha); // True } } |
3. Using foreach
If you are not allowed to use LINQ or Regex, try using the below code. It uses a basic foreach loop to traverse the string and determine if each character is a letter or not. The following code checks for Unicode letters, but it can be easily modified to check for ASCII letters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; public static class StringExtensions { public static bool isAlpha(this string input) { foreach (char ch in input) { if (!Char.IsLetter(ch)) { return false; } } return true; } } public class Example { public static void Main() { string s = "Hello"; bool isAlpha = s.isAlpha(); Console.WriteLine(isAlpha); // True } } |
That’s all about checking if a string contains only letters in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)