Open In App

C# | Char.IsLetterOrDigit() Method

In C#, Char.IsLetterOrDigit() is a System.Char struct method which is used to check whether a Unicode character can be categorized as a letter or decimal digit. Valid letters and decimal digits will be the members of the UnicodeCategory: UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, OtherLetter, or DecimalDigitNumber category. This method can be overloaded by passing different type and number of arguments to it.



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

Char.IsLetterOrDigit(Char) Method

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



Syntax:

public static bool IsLetterOrDigit(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 Unicode letter or decimal digit, otherwise returns False. The return type of this method is System.Boolean.

Example:




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

Output:
True
False

Char.IsLetterOrDigit(String, Int32) Method

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

Syntax:

public static bool IsLetterOrDigit(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 letter or decimal digit at the specified index in the specified string, otherwise returns False. The return type of this method is System.Boolean.

Exceptions:

Example:




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

Output:
True
False

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


Article Tags :
C#