Home
C#
char.IsDigit Example
This page was last reviewed on May 31, 2021.
Dot Net Perls
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.
char
Input and output. Consider the string "abc123." The char.IsDigit method should return false 3 times, and then true 3 times, in a string loop.
Loop, String Chars
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.
return bool
static
Also It contains some logic for globalization, which checks for Latin characters.
Info The program defines the Main entry point and an input string. The string contains lowercase letters, punctuation, and digits.
And In the output, IsDigit returns True only for the digits. It accurately detects digit chars.
true, false
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).
Home
Changes
© 2007-2024 Sam Allen.