В большинстве языков программирования вы можете использовать функцию/метод isdigit()
, чтобы проверить, является ли символ цифрой. Вот несколько примеров на популярных языках программирования:
-
Python:
char = '5' if char.isdigit(): print("Character is a digit") else: print("Character is not a digit")
-
Java:
char ch = '5'; if (Character.isDigit(ch)) { System.out.println("Character is a digit"); } else { System.out.println("Character is not a digit"); }
-
C++:
char ch = '5'; if (isdigit(ch)) { cout << "Character is a digit" << endl; } else { cout << "Character is not a digit" << endl; }
-
JavaScript:
var char = '5'; if (char.match(/^\d$/)) { console.log("Character is a digit"); } else { console.log("Character is not a digit"); }
-
C#:
char ch = '5'; if (char.IsDigit(ch)) { Console.WriteLine("Character is a digit"); } else { Console.WriteLine("Character is not a digit"); }