Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Char Struct in C#
The Char struct in C# represents a single Unicode character as a UTF-16 code unit. It provides numerous static methods for character classification, conversion, and comparison operations. The Char struct is a value type that implements IComparable, IConvertible, and IEquatable<char> interfaces.
Key Char Methods
| Method | Description |
|---|---|
| ConvertToUtf32(Char, Char) | Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point. |
| ConvertToUtf32(String, Int32) | Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point. |
| Equals(Char) | Returns a value that indicates whether this instance is equal to the specified Char object. |
| GetNumericValue(Char) | Converts the specified numeric Unicode character to a double-precision floating-point number. |
| IsDigit(String, Int32) | Indicates whether the character at the specified position in a specified string is categorized as a decimal digit. |
| IsLetter(Char) | Indicates whether the specified Unicode character is categorized as a Unicode letter. |
| IsLetterOrDigit(Char) | Indicates whether the specified Unicode character is categorized as a letter or a decimal digit. |
| IsLower(Char) | Indicates whether the specified Unicode character is categorized as a lowercase letter. |
| IsPunctuation(String, Int32) | Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark. |
Using Char.IsSymbol() Method
The Char.IsSymbol() method determines whether a character is categorized as a symbol character (such as mathematical symbols, currency symbols, or other symbolic characters).
Syntax
public static bool IsSymbol(char ch); public static bool IsSymbol(string str, int index);
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = 'P';
Console.WriteLine("Value = " + val);
res = Char.IsSymbol(val);
Console.WriteLine("Is the value a symbol? = " + res);
char symbol = '$';
Console.WriteLine("Value = " + symbol);
res = Char.IsSymbol(symbol);
Console.WriteLine("Is the value a symbol? = " + res);
}
}
The output of the above code is −
Value = P Is the value a symbol? = False Value = $ Is the value a symbol? = True
Using Char.IsWhiteSpace() Method
The Char.IsWhiteSpace() method identifies whether a character is a white-space character, including spaces, tabs, line feeds, and other Unicode white-space characters.
Syntax
public static bool IsWhiteSpace(char ch); public static bool IsWhiteSpace(string str, int index);
Example
using System;
public class Demo {
public static void Main() {
bool res;
char val = ' ';
Console.WriteLine("Testing space character:");
res = Char.IsWhiteSpace(val);
Console.WriteLine("Is the value whitespace? = " + res);
char letter = 'A';
Console.WriteLine("Testing letter 'A':");
res = Char.IsWhiteSpace(letter);
Console.WriteLine("Is the value whitespace? = " + res);
char tab = '\t';
Console.WriteLine("Testing tab character:");
res = Char.IsWhiteSpace(tab);
Console.WriteLine("Is the value whitespace? = " + res);
}
}
The output of the above code is −
Testing space character: Is the value whitespace? = True Testing letter 'A': Is the value whitespace? = False Testing tab character: Is the value whitespace? = True
Using Char Classification Methods
Example
using System;
public class Demo {
public static void Main() {
char[] testChars = {'A', '5', '$', ' ', '!', 'z'};
foreach (char ch in testChars) {
Console.WriteLine($"Character: '{ch}'");
Console.WriteLine($" IsLetter: {Char.IsLetter(ch)}");
Console.WriteLine($" IsDigit: {Char.IsDigit(ch)}");
Console.WriteLine($" IsSymbol: {Char.IsSymbol(ch)}");
Console.WriteLine($" IsPunctuation: {Char.IsPunctuation(ch)}");
Console.WriteLine($" IsLower: {Char.IsLower(ch)}");
Console.WriteLine();
}
}
}
The output of the above code is −
Character: 'A' IsLetter: True IsDigit: False IsSymbol: False IsPunctuation: False IsLower: False Character: '5' IsLetter: False IsDigit: True IsSymbol: False IsPunctuation: False IsLower: False Character: '$' IsLetter: False IsDigit: False IsSymbol: True IsPunctuation: False IsLower: False Character: ' ' IsLetter: False IsDigit: False IsSymbol: False IsPunctuation: False IsLower: False Character: '!' IsLetter: False IsDigit: False IsSymbol: False IsPunctuation: True IsLower: False Character: 'z' IsLetter: True IsDigit: False IsSymbol: False IsPunctuation: False IsLower: True
Conclusion
The Char struct in C# provides comprehensive methods for character analysis and classification. These methods are essential for text processing, input validation, and string manipulation tasks, allowing developers to categorize characters as letters, digits, symbols, punctuation, or whitespace with precision.
