C# Reference · Strings and Text
C# Char methods
Reference for the static char methods in C#: IsLetter, IsDigit, IsLetterOrDigit, IsWhiteSpace, IsPunctuation, ToUpper, GetNumericValue and surrogate handling.
The char struct has a family of static methods that classify a character (is it a letter, a digit, whitespace, punctuation?) and convert its case. They are the building blocks of tokenizers, validators and simple text cleanup. Each classification method has two overloads: one taking a char, and one taking a string and an index.
These methods follow the Unicode character database, so char.IsLetter('é') and char.IsLetter('ж') are both true. They work on UTF-16 code units: a character outside the Basic Multilingual Plane is two chars and each half is reported as a surrogate, not as a letter. For whole code points use Rune.
Console.WriteLine(char.IsDigit('7')); // True Console.WriteLine(char.ToUpper('q')); // Q
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static bool IsLetter(char c) public static bool IsDigit(char c) public static bool IsLetterOrDigit(char c) public static bool IsWhiteSpace(char c) public static bool IsUpper(char c) public static bool IsLower(char c) public static bool IsPunctuation(char c) public static bool IsSymbol(char c) public static bool IsControl(char c) public static bool IsSurrogate(char c) public static bool IsAsciiDigit(char c) public static bool IsAsciiLetter(char c) public static char ToUpper(char c) public static char ToLower(char c) public static char ToUpperInvariant(char c) public static double GetNumericValue(char c) public static UnicodeCategory GetUnicodeCategory(char c) public static int ConvertToUtf32(string s, int index) public static string ConvertFromUtf32(int utf32)
| Name | Description |
|---|---|
c |
The character to test or convert. |
s, index |
For the indexed overloads: the string and the position of the char to examine. |
utf32 |
A Unicode code point from 0 to 0x10FFFF, excluding surrogates. |
Returns: bool for the Is... methods; the converted char for ToUpper/ToLower; GetNumericValue returns -1 for characters that are not numeric.
Errors / undefined behavior: the indexed overloads throw ArgumentNullException for a null string and ArgumentOutOfRangeException for a bad index. ConvertFromUtf32 throws for surrogate or out-of-range values.
Usage notes
IsDigitis wider than0-9. It accepts digits from other scripts (Arabic-Indic, Devanagari). For validating ASCII numbers usechar.IsAsciiDigit, or comparec is >= '0' and <= '9'.IsLetteris not "A-Z". It includes accented and non-Latin letters.char.IsAsciiLetteris the ASCII-only version (.NET 7+).IsPunctuationversusIsSymbol.$,+,<,=and^are symbols;-,_,!,,are punctuation. If you need "anything that is not a letter, digit or space", testIsLetterOrDigitandIsWhiteSpaceinstead.IsWhiteSpaceincludes more than the space. Tab, newline, no-break space and Unicode spaces count; the zero-width space does not.- Prefer
ToUpperInvariant/ToLowerInvariantfor identifiers, keys and protocols.ToUpper(char)uses the current culture, which is invariant in our runner but not on many machines (the Turkishiproblem). - One
charcannot always change case.'ß'has no single-char uppercase, so it is unchanged, and the string version"straße".ToUpperInvariant()also keeps theß(it printsSTRAßEin the example). .NET uses simple, one-to-one case mapping. - Surrogates. Looping over
charsplits emoji into halves that fail every classification. UseEnumerateRunes()and theRunemethods when your text can contain them. - LINQ with method groups.
text.Count(char.IsLetter)andtext.Where(char.IsDigit)read nicely and need no lambda.