In this article, we will discuss the Char.IsLetterOrDigit method in C# with syntax and examples.
What is Char.IsLetterOrDigit method?
In C#, Char.IsLetterOrDigit is a Char struct method that determines whether a Unicode character belongs to a character or decimal digit class.
Unicode class members:
There are several Unicode class members, including uppercase, lowercase, title characters, transition characters, new characters, and decimal digit numbers. This method can be overloaded by returning different numbers of arguments.
Syntax:
It has the following syntax:
Public static bool IsLetterOrDigit (char c );
Where char c is the Unicode character to evaluate.
Return Type:
The return type of these methods is Boolean . If the method matches any Unicode letter or decimal digit, it returns True, and if the method does not match any Unicode letter or decimal digit, it returns False.
Pseudocode:
function IsLetterOrDigit(character):
if the character is a letter:
return true
else if the character is a digit:
return true
else:
return false
character = 'a' // Example character
isLetterOrDigit = IsLetterOrDigit(character)
output(isLetterOrDigit) // Output: True
Program 1:
Let us take an example to illustrate the Char.IsLetterOrDigit method in C#.
using System;
class ABC {
// Main Method
static public void Main()
{
// Declaration of data type
bool res;
// checking if P is a
// letter or decimal digit
char ch1 = 'P';
res = Char.IsLetterOrDigit(ch1);
Console.WriteLine(res);
// checking if '$' is a
// letter or decimal digit
char ch2 = '$';
res = Char.IsLetterOrDigit(ch2);
Console.WriteLine(res);
}
}
Output:
Program 2:
Let us take another example to illustrate the Char.IsLetterOrDigit method in C#.
using System;
class Program
{
static void Main()
{
char[] characters = { 'p', '9', '@', ' ' };
foreach (char ch in characters)
{
bool isLetterOrDigit = Char.IsLetterOrDigit(ch);
Console.WriteLine($"Character '{ch}' is a letter or digit: {isLetterOrDigit}");
}
}
}
Output:
Another method that is used for the same process is Char.IsLetterOrDigit (String, Int) method.
Char.IsLetterOrDigit (String, Int) method:
The Char.IsLetterOrDigit (String, Int) method is method is used to determine whether a given string at a particular position matches a character or decimal place. It returns True value if it matches a character or decimal place, if it does not match, it will return false.
Syntax:
It has the following syntax:
public static bool IsLetterOrDigit ( string str, int index );
The parameters are,
Str: It is the System's mandatory System.string type that has to be evaluated.
Index: The character's position within the string that needs to be compared, and the parameter's type is System.Int32 .
The return type of this method is System.Boolean . If the method matches any letter or decimal digit at the given index in the given string, it returns True, if it does not match, it returns False.
The exceptions for this method are,
- If the str value is null, the method throws an ArgumentNullException .
- If the index is greater than the last position in str or less than zero, the ArgumentOutOfRangeException is thrown by this function.
Pseudocode:
function IsLetterOrDigit(string, index):
if the index is out of range for string:
return false
else:
character = string[index]
if the character is a letter:
return true
else if the character is a digit:
return true
else:
return false
string = "abc123"
index = 4
isLetterOrDigit = IsLetterOrDigit(string, index)
output(isLetterOrDigit) // Output: True
Program 1:
Let us take an example to illustrate the Char.IsLetterOrDigit (String, Int) method in C#.
using System;
public class Program {
public static void Main(){
bool res;
char val = '$';
Console.WriteLine("Value = "+val);
res = Char.IsLetterOrDigit(val);
Console.WriteLine(" "+res);
}
}
Output:
Program 2:
Let us take another example to illustrate the Char.IsLetterOrDigit (String, Int) method in C#.
using System;
class ABC {
// Main Method
static public void Main()
{
// Declaration of data type
bool res;
// checking for letter or decimal digit
// in a string at the desired position
string str1 = "Hello123";
res = Char.IsLetterOrDigit(str1, 2);
Console.WriteLine(res);
// checking for letter or decimal digit
// in a string at a desired position
string str2 = "Hello@world";
res = Char.IsLetterOrDigit(str2, 3);
Console.WriteLine(res);
}
}
Output:
Advantages of Char.IsLetterOrDigit method:
There are several advantages of the Char.IsLetterOrDigit method in C#. Some main advantages of the Char.IsLetterOrDigit method are as follows:
- Simplicity: It eliminates the need for complex conditional statements and provides a straightforward method of determining if a character is a letter or a numeric.
- Readability: By using this method, the code's purpose is made clear, which makes it simpler for other developers to understand its goal.
- Localization: The method takes into account the current culture settings, which can be important for applications that need to handle different languages and character sets
- Consistency: By following the Unicode standard for character classification, this approach guarantees consistency when checking for letters and digits.
- Performance: The approach is probably efficient for use in performance-sensitive applications because it has been optimized for performance.
- Safety: Checking for letters and numbers, helps avoid typical programming mistakes such as accidentally utilizing the wrong character ranges or forgetting to include specific characters.