Skip to content
Tutorials Center
My learning

Topics

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.

C#
Console.WriteLine(char.IsDigit('7'));   // True
Console.WriteLine(char.ToUpper('q'));   // Q

Try it

C# Classifying characters

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.

C# Case conversion and a small tokenizer

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

C#
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

  • IsDigit is wider than 0-9. It accepts digits from other scripts (Arabic-Indic, Devanagari). For validating ASCII numbers use char.IsAsciiDigit, or compare c is >= '0' and <= '9'.
  • IsLetter is not "A-Z". It includes accented and non-Latin letters. char.IsAsciiLetter is the ASCII-only version (.NET 7+).
  • IsPunctuation versus IsSymbol. $, +, <, = and ^ are symbols; -, _, !, , are punctuation. If you need "anything that is not a letter, digit or space", test IsLetterOrDigit and IsWhiteSpace instead.
  • IsWhiteSpace includes more than the space. Tab, newline, no-break space and Unicode spaces count; the zero-width space does not.
  • Prefer ToUpperInvariant/ToLowerInvariant for identifiers, keys and protocols. ToUpper(char) uses the current culture, which is invariant in our runner but not on many machines (the Turkish i problem).
  • One char cannot always change case. 'ß' has no single-char uppercase, so it is unchanged, and the string version "straße".ToUpperInvariant() also keeps the ß (it prints STRAßE in the example). .NET uses simple, one-to-one case mapping.
  • Surrogates. Looping over char splits emoji into halves that fail every classification. Use EnumerateRunes() and the Rune methods when your text can contain them.
  • LINQ with method groups. text.Count(char.IsLetter) and text.Where(char.IsDigit) read nicely and need no lambda.