TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# char.IsDigit (If Char Is Between 0 and 9)

Invoke the char.IsDigit static method. A digit char is between 0 and 9.
Char.IsDigit. This 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. It is useful in parsing, scanning or sorting algorithms.Char
First, the char.IsDigit method is a static method on the System.Char struct, which is aliased to the char keyword in the C# programming language. It returns a Boolean value, which tells you whether the character is a digit character.Bool MethodStatic

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
C# 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
Unicode. As Raymond Chen at Microsoft notes, the IsDigit method has some interesting behavior on non-Latin numbers, meaning certain rare Unicode characters. This is mainly a security concern when using it in input validation.

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.

Summary. The char.IsDigit method works in normal situations with digits between 0 and 9. We used it in a foreach-loop. It has an unusual behavior that complicates its behavior. This could lead to unexpected behavior.

Tip: You can extract the character-testing logic for more predictable behavior and slightly better performance.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf