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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 31, 2021 (image).