Char.IsDigit. This C# method checks character values. It determines if a character in the string is a digit character—meaning it is part of an Arabic number—in the range 0-9.
C# method uses. IsDigit is useful in parsing, scanning or sorting algorithms. It simply tests the range of the char value. And this logic can be extracted from IsDigit.
a -> false
b -> false
c -> false
1 -> true
2 -> true
3 -> true
First example. IsDigit() is a static method on the System.Char struct, which is aliased to the char keyword. It returns true if the character is a digit character.
using System;
class Program
{
static void Main()
{
string test = "Abc,123";
foreach (char value in test)
{
bool digit = char.IsDigit(value);
Console.Write(value);
Console.Write(' ');
Console.WriteLine(digit);
}
}
/// <summary>/// Returns whether the char is a digit char./// Taken from inside the char.IsDigit method./// </summary>
public static bool IsCharDigit(char c)
{
return ((c >= '0') && (c <= '9'));
}
}A False
b False
c False
, False
1 True
2 True
3 True
A summary. The C# char.IsDigit method tests for digits between 0 and 9. You can extract the character-testing logic for more predictable behavior and slightly better performance.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 31, 2021 (image).