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.IsDigit() Method in C#
The Char.IsDigit() method in C# determines whether a specified Unicode character is categorized as a decimal digit. This method is useful for validating input, parsing strings, and filtering numeric characters from text.
Syntax
Following is the syntax for the Char.IsDigit() method −
public static bool IsDigit(char ch);
Parameters
-
ch − The Unicode character to evaluate.
Return Value
Returns true if the character is a decimal digit; otherwise, false.
Using Char.IsDigit() with Different Characters
Example 1 - Testing Non-Digit Characters
using System;
public class Demo {
public static void Main() {
bool res;
char val = 'g';
Console.WriteLine("Value = " + val);
res = Char.IsDigit(val);
Console.WriteLine("Is the value a digit? = " + res);
}
}
The output of the above code is −
Value = g Is the value a digit? = False
Example 2 - Testing Digit Characters
using System;
public class Demo {
public static void Main() {
bool res;
char val = '2';
Console.WriteLine("Value = " + val);
res = Char.IsDigit(val);
Console.WriteLine("Is the value a digit? = " + res);
}
}
The output of the above code is −
Value = 2 Is the value a digit? = True
Testing Multiple Characters
Example
using System;
public class Demo {
public static void Main() {
char[] testChars = {'5', 'A', '@', '9', ' ', '0'};
Console.WriteLine("Character\tIs Digit?");
Console.WriteLine("-------------------");
foreach (char ch in testChars) {
bool isDigit = Char.IsDigit(ch);
Console.WriteLine($"'{ch}'\t\t{isDigit}");
}
}
}
The output of the above code is −
Character Is Digit? ------------------- '5' True 'A' False '@' False '9' True ' ' False '0' True
Common Use Cases
-
Input validation − Checking if user input contains only numeric characters.
-
String parsing − Extracting numeric characters from mixed text.
-
Data filtering − Separating digits from non-digit characters in processing operations.
Conclusion
The Char.IsDigit() method provides a reliable way to check if a character represents a decimal digit (0-9). It returns true for digit characters and false for all other characters, making it essential for character validation and text processing tasks.
