C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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, FalseC# program that tests for digits
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'));
}
}
Output
A False
b False
c False
, False
1 True
2 True
3 True
Therefore: Using a custom IsCharDigit method is safer for simple tasks. The logic in IsCharDigit is taken directly from the main part of char.IsDigit.
Tip: You can extract the character-testing logic for more predictable behavior and slightly better performance.