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
Char.IsLetterOrDigit() Method in C#
The Char.IsLetterOrDigit() method in C# determines whether a specified Unicode character is categorized as a letter or a decimal digit. This method is useful for validating input characters, parsing strings, and filtering alphanumeric content.
Syntax
Following is the syntax for the Char.IsLetterOrDigit() method −
public static bool IsLetterOrDigit(char ch);
There is also an overloaded version that works with strings −
public static bool IsLetterOrDigit(string s, int index);
Parameters
ch − The Unicode character to evaluate.
s − A string.
index − The position of the character to evaluate in the string.
Return Value
Returns true if the character is a letter or digit; otherwise, false.
Using IsLetterOrDigit() with Individual Characters
Example with Digit Character
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
Example with Special Character
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
Using IsLetterOrDigit() with Multiple Characters
Example with Mixed Character Types
using System;
public class Demo {
public static void Main() {
char[] chars = {'A', '5', '@', 'z', '0', ' ', 'X'};
Console.WriteLine("Character\tIsLetterOrDigit");
Console.WriteLine("-------------------------");
foreach (char ch in chars) {
bool result = Char.IsLetterOrDigit(ch);
Console.WriteLine($"{ch}\t\t{result}");
}
}
}
The output of the above code is −
Character IsLetterOrDigit ------------------------- A True 5 True @ False z True 0 True False X True
Using IsLetterOrDigit() with String Index
Example with String Validation
using System;
public class Demo {
public static void Main() {
string text = "Hello123!";
Console.WriteLine($"Analyzing string: '{text}'");
Console.WriteLine("Index\tChar\tIsLetterOrDigit");
Console.WriteLine("-----------------------------");
for (int i = 0; i < text.Length; i++) {
bool result = Char.IsLetterOrDigit(text, i);
Console.WriteLine($"{i}\t{text[i]}\t{result}");
}
}
}
The output of the above code is −
Analyzing string: 'Hello123!' Index Char IsLetterOrDigit ----------------------------- 0 H True 1 e True 2 l True 3 l True 4 o True 5 1 True 6 2 True 7 3 True 8 ! False
Common Use Cases
Input Validation − Checking if user input contains only alphanumeric characters.
String Parsing − Filtering out non-alphanumeric characters from text.
Data Cleaning − Removing special characters while preserving letters and digits.
Username Validation − Ensuring usernames contain only valid characters.
Conclusion
The Char.IsLetterOrDigit() method provides an efficient way to check if a character is either a letter or a digit. It supports both individual character checking and string-based character validation, making it versatile for input validation and text processing scenarios.
