Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check whether the specified Unicode character is a letter or a decimal digit in C#
To check whether the specified Unicode character is a letter or a decimal digit in C#, use the Char.IsLetterOrDigit() method. This method returns true if the character is a letter (uppercase or lowercase) or a decimal digit (0-9), and false otherwise.
Syntax
Following is the syntax for the Char.IsLetterOrDigit() method −
public static bool IsLetterOrDigit(char c)
Parameters
-
c: The Unicode character to evaluate.
Return Value
Returns true if the character is a letter or decimal digit; otherwise, false.
Using IsLetterOrDigit with Decimal Digits
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = '1';
Console.WriteLine("Value = " + val);
res = Char.IsLetterOrDigit(val);
Console.WriteLine("Is the value a letter or digit? = " + res);
}
}
The output of the above code is −
Value = 1 Is the value a letter or digit? = True
Using IsLetterOrDigit with Special Characters
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = '$';
Console.WriteLine("Value = " + val);
res = Char.IsLetterOrDigit(val);
Console.WriteLine("Is the value a letter or digit? = " + res);
}
}
The output of the above code is −
Value = $ Is the value a letter or digit? = False
Comprehensive Example with Different Character Types
Example
using System;
public class Demo {
public static void Main() {
char[] testChars = {'A', 'z', '5', '@', ' ', 'ñ', '²'};
Console.WriteLine("Character\tIsLetterOrDigit");
Console.WriteLine("-------------------------");
foreach(char ch in testChars) {
bool result = Char.IsLetterOrDigit(ch);
Console.WriteLine($"{ch}\t\t{result}");
}
}
}
The output of the above code is −
Character IsLetterOrDigit ------------------------- A True z True 5 True @ False False ñ True ² True
Conclusion
The Char.IsLetterOrDigit() method provides a simple way to validate whether a Unicode character is alphanumeric. It returns true for letters (including accented characters) and decimal digits, making it useful for input validation and character classification in C# applications.
