C# | Char.IsDigit() Method

In C#, Char.IsDigit() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a decimal digit(radix 10) or not. Valid digits will be the members of the UnicodeCategory.DecimalDigitNumber category. This method can be overloaded by passing different type and number of arguments to it.

  1. Char.IsDigit(Char) Method
  2. Char.IsDigit(String, Int32) Method



Char.IsDigit(Char) Method

This method is used to check whether the specified Unicode character matches decimal digit or not. If it matches then it returns True otherwise return False.

Syntax:

public static bool IsDigit(char ch);

Parameter:

ch: It is required Unicode character of System.char type which is to be checked.

Return Type: The method returns True, if it successfully matches any decimal digit, otherwise returns False. The return type of this method is System.Boolean.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Char.IsDigit(Char) Method
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Declaration of data type
        bool result;
  
        // checking if 5
        // is a digit or not
        char ch1 = '5';
        result = Char.IsDigit(ch1);
        Console.WriteLine(result);
  
        // checking if 'c'
        // is a digit
        char ch2 = 'c';
        result = Char.IsDigit(ch2);
        Console.WriteLine(result);
    }
}

chevron_right


Output:

True
False

Char.IsDigit(String, Int32) Method

This method is used to check whether the specified string at specified position matches with any decimal digit or not. If it matches then it returns True otherwise returns False.

Syntax:

public static bool IsDigit(string str, int index);

Parameters:


Str: It is the required string of System.String type which is to be evaluate.
index: It is the position of character in string to be compared and type of this parameter is System.Int32.

Return Type: The method returns True if it successfully matches any decimal digit at the specified index in the specified string, otherwise returns False. The return type of this method is System.Boolean.

Exceptions:

  • If the value of str is null then this method will give ArgumentNullException
  • If the index is less than zero or greater than the last position in str then this method will give ArgumentOutOfRangeException.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Char.IsDigit(String, Int32) Method
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Declaration of data type
        bool result;
  
        // checking for decimal digit in
        // a string at a desired position
        string str1 = "GeeksforGeeks";
        result = Char.IsDigit(str1, 2);
        Console.WriteLine(result);
  
        // checking for decimal digit in a
        // string at a desired position
        string str2 = "geeks5forgeeks";
        result = Char.IsDigit(str2, 5);
        Console.WriteLine(result);
    }
}

chevron_right


Output:

False
True

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.char.isdigit?view=netframework-4.7.2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.