[Serializable] |
Char provides methods to compare instances of this type, convert the value of an instance to its string representation, convert the string representation of a number to an instance of this type, and determine whether an instance is in a category such as digit, letter, punctuation, control character, and so on.
For information about how format specification codes control the string representation of value types, see the conceptual topic at MSDN: formattingoverview.
For more information about Unicode, see the Unicode Standard at http://www.unicode.org.
This type implements interfaces IComparable and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.
using System; public class CharStructureSample { public static void Main() { char chA = 'A'; char ch1 = '1'; string str = "test string"; Console.WriteLine(chA.CompareTo('B')); // Output: "-1" (meaning 'A' is 1 less than 'B') Console.WriteLine(chA.Equals('A')); // Output: "True" Console.WriteLine(Char.GetNumericValue(ch1)); // Output: "1" Console.WriteLine(Char.IsControl('\t')); // Output: "True" Console.WriteLine(Char.IsDigit(ch1)); // Output: "True" Console.WriteLine(Char.IsLetter(',')); // Output: "False" Console.WriteLine(Char.IsLower('u')); // Output: "True" Console.WriteLine(Char.IsNumber(ch1)); // Output: "True" Console.WriteLine(Char.IsPunctuation('.')); // Output: "True" Console.WriteLine(Char.IsSeparator(str, 4)); // Output: "True" Console.WriteLine(Char.IsSymbol('+')); // Output: "True" Console.WriteLine(Char.IsWhiteSpace(str, 4)); // Output: "True" Console.WriteLine(Char.Parse("S")); // Output: "S" Console.WriteLine(Char.ToLower('M')); // Output: "m" Console.WriteLine('x'.ToString()); // Output: "x" } }
MaxValue | Represents the largest possible value of a Char. This field is constant. |
MinValue | Represents the smallest possible value of a Char. This field is constant. |
CompareTo | Compares this instance to a specified object and returns an indication of their relative values. |
Equals | Overridden: Returns a value indicating whether this instance is equal to a specified object. |
GetHashCode | Overridden: Returns the hash code for this instance. |
GetNumericValue | Overloaded:GetNumericValue(char c) Converts the specified numeric Unicode character to a double-precision floating point number. |
GetNumericValue | Overloaded:GetNumericValue(string s, int index) Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
GetTypeCode | Returns the TypeCode for value type Char. |
GetUnicodeCategory | Overloaded:GetUnicodeCategory(char c) Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values. |
GetUnicodeCategory | Overloaded:GetUnicodeCategory(string s, int index) Categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values. |
IsControl | Overloaded:IsControl(char c) Indicates whether the specified Unicode character is categorized as a control character. |
IsControl | Overloaded:IsControl(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a control character. |
IsDigit | Overloaded:IsDigit(char c) Indicates whether the specified Unicode character is categorized as a decimal digit. |
IsDigit | Overloaded:IsDigit(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a decimal digit. |
IsLetter | Overloaded:IsLetter(char c) Indicates whether the specified Unicode character is categorized as an alphabetic letter. |
IsLetter | Overloaded:IsLetter(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as an alphabetic character. |
IsLetterOrDigit | Overloaded:IsLetterOrDigit(char c) Indicates whether the specified Unicode character is categorized as an alphabetic letter or a decimal digit. |
IsLetterOrDigit | Overloaded:IsLetterOrDigit(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as an alphabetic character or a decimal digit. |
IsLower | Overloaded:IsLower(char c) Indicates whether the specified Unicode character is categorized as a lowercase letter. |
IsLower | Overloaded:IsLower(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter. |
IsNumber | Overloaded:IsNumber(char c) Indicates whether the specified Unicode character is categorized as a decimal digit or hexadecimal number. |
IsNumber | Overloaded:IsNumber(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a decimal digit or hexadecimal number. |
IsPunctuation | Overloaded:IsPunctuation(char c) Indicates whether the specified Unicode character is categorized as a punctuation mark. |
IsPunctuation | Overloaded:IsPunctuation(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark. |
IsSeparator | Overloaded:IsSeparator(char c) Indicates whether the specified Unicode character is categorized as a separator character. |
IsSeparator | Overloaded:IsSeparator(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a separator character. |
IsSurrogate | Overloaded:IsSurrogate(char c) Indicates whether the specified Unicode character is categorized as a surrogate character. |
IsSurrogate | Overloaded:IsSurrogate(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a surrogate character. |
IsSymbol | Overloaded:IsSymbol(char c) Indicates whether the specified Unicode character is categorized as a symbol character. |
IsSymbol | Overloaded:IsSymbol(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as a symbol character. |
IsUpper | Overloaded:IsUpper(char c) Indicates whether the specified Unicode character is categorized as an uppercase letter. |
IsUpper | Overloaded:IsUpper(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter. |
IsWhiteSpace | Overloaded:IsWhiteSpace(char c) Indicates whether the specified Unicode character is categorized as white space. |
IsWhiteSpace | Overloaded:IsWhiteSpace(string s, int index) Indicates whether the character at the specified position in a specified string is categorized as white space. |
Parse | Converts the value of the specified string to its equivalent Unicode character. |
ToLower | Overloaded:ToLower(char c) Converts the value of a Unicode character to its lowercase equivalent. |
ToLower | Overloaded:ToLower(char c, CultureInfo culture) Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information. |
ToString | Overloaded:ToString() Overridden: Converts the value of this instance to its equivalent string representation. |
ToString | Overloaded:ToString(char c) Converts the specified Unicode character to its equivalent string representation. |
ToString | Overloaded:ToString(IFormatProvider provider) Converts the value of this instance to its equivalent string representation using the specified culture-specific format information. |
ToUpper | Overloaded:ToUpper(char c) Converts the value of a Unicode character to its uppercase equivalent. |
ToUpper | Overloaded:ToUpper(char c, CultureInfo culture) Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public const char MaxValue;
|
public const char MinValue;
|
value
Return Value | Description |
---|---|
Less than zero | This instance is less than . |
Zero | This instance is equal to . |
Greater than zero | This instance is greater than value. -or- value is null. |
Exception Type | Condition |
---|---|
ArgumentException | value is not a Char or null. |
value must be null or an instance of Char; otherwise, an exception is thrown.
The comparison performed by this method is based on the encoded values of this instance and value, not necessarily their lexicographical characteristics.
This method is implemented to support the IComparable interface.
using System; public class CompareToSample { public static void Main() { char chA = 'A'; char chB = 'B'; Console.WriteLine(chA.CompareTo('A')); // Output: "0" (meaning they're equal) Console.WriteLine('b'.CompareTo(chB)); // Output: "32" (meaning 'b' is greater than 'B' by 32) Console.WriteLine(chA.CompareTo(chB)); // Output: "-1" (meaning 'A' is less than 'B' by 1) } }
obj
using System; public class EqualsSample { public static void Main() { char chA = 'A'; char chB = 'B'; Console.WriteLine(chA.Equals('A')); // Output: "True" Console.WriteLine('b'.Equals(chB)); // Output: "False" } }
~Char(); |
public override int GetHashCode(); |
c
A character has an associated numeric value if and only if it is a member of one of the following categories in UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
using System; public class GetNumericValueSample { public static void Main() { string str = "input: 1"; Console.WriteLine(Char.GetNumericValue('8')); // Output: "8" Console.WriteLine(Char.GetNumericValue(str, 8)); // Output: "1" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Character positions in a string are indexed starting from zero.
A character has an associated numeric value if and only if it is a member of one of the following categories in UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
using System; public class GetNumericValueSample { public static void Main() { string str = "input: 1"; Console.WriteLine(Char.GetNumericValue('8')); // Output: "8" Console.WriteLine(Char.GetNumericValue(str, 8)); // Output: "1" } }
public Type GetType(); |
public TypeCode GetTypeCode(); |
public static UnicodeCategory GetUnicodeCategory( |
c
public static UnicodeCategory GetUnicodeCategory( |
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
c
using System; public class IsControlSample { public static void Main() { string str = "sample string"; Console.WriteLine(Char.IsControl('\t')); // Output: "True" Console.WriteLine(Char.IsControl(str, 7)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid control characters are members of the following category in UnicodeCategory: Control.
using System; public class IsControlSample { public static void Main() { string str = "sample string"; Console.WriteLine(Char.IsControl('\t')); // Output: "True" Console.WriteLine(Char.IsControl(str, 7)); // Output: "False" } }
c
Valid digits are members of the following category in UnicodeCategory: DecimalDigitNumber.
using System; public class IsDigitSample { public static void Main() { char ch = '8'; Console.WriteLine(Char.IsDigit(ch)); // Output: "True" Console.WriteLine(Char.IsDigit("sample string", 7)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Character positions in a string are indexed starting from zero.
Valid digits are members of the following category in UnicodeCategory: DecimalDigitNumber.
using System; public class IsDigitSample { public static void Main() { char ch = '8'; Console.WriteLine(Char.IsDigit(ch)); // Output: "True" Console.WriteLine(Char.IsDigit("sample string", 7)); // Output: "False" } }
c
using System; public class IsLetterSample { public static void Main() { char ch = '8'; Console.WriteLine(Char.IsLetter(ch)); // False Console.WriteLine(Char.IsLetter("sample string", 7)); // True } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid letters are members of the following categories in UnicodeCategory: UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, and OtherLetter.
using System; public class IsLetterSample { public static void Main() { char ch = '8'; Console.WriteLine(Char.IsLetter(ch)); // False Console.WriteLine(Char.IsLetter("sample string", 7)); // True } }
c
using System; public class IsLetterOrDigitSample { public static void Main() { string str = "newline:\n"; Console.WriteLine(Char.IsLetterOrDigit('8')); // Output: "True" Console.WriteLine(Char.IsLetterOrDigit(str, 8)); // Output: "False", because it's a newline } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid alphabetic letters and decimal digits are members of the following categories in UnicodeCategory: UppercaseLetter, LowercaseLetter, TitlecaseLetter, ModifierLetter, OtherLetter, or DecimalDigitNumber.
using System; public class IsLetterOrDigitSample { public static void Main() { string str = "newline:\n"; Console.WriteLine(Char.IsLetterOrDigit('8')); // Output: "True" Console.WriteLine(Char.IsLetterOrDigit(str, 8)); // Output: "False", because it's a newline } }
c
using System; public class IsLowerSample { public static void Main() { char ch = 'a'; Console.WriteLine(Char.IsLower(ch)); // Output: "True" Console.WriteLine(Char.IsLower("upperCase", 5)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid lowercase letters are members of the following category in UnicodeCategory: LowercaseLetter.
using System; public class IsLowerSample { public static void Main() { char ch = 'a'; Console.WriteLine(Char.IsLower(ch)); // Output: "True" Console.WriteLine(Char.IsLower("upperCase", 5)); // Output: "False" } }
c
Valid numbers are members of the following categories in UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
using System; public class IsNumberSample { public static void Main() { string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True" Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Character positions in a string are indexed starting from zero.
Valid numbers are members of the following categories in UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
using System; public class IsNumberSample { public static void Main() { string str = "non-numeric"; Console.WriteLine(Char.IsNumber('8')); // Output: "True" Console.WriteLine(Char.IsNumber(str, 3)); // Output: "False" } }
c
using System; public class IsPunctuationSample { public static void Main() { char ch = '.'; Console.WriteLine(Char.IsPunctuation(ch)); // Output: "True" Console.WriteLine(Char.IsPunctuation("no punctuation", 3)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid punctuation marks are members of the following categories in UnicodeCategory: ConnectorPunctuation, DashPunctuation, OpenPunctuation, ClosePunctuation, InititalQuotePunctuation, FinalQuotePunctuation, or OtherPunctuation.
using System; public class IsPunctuationSample { public static void Main() { char ch = '.'; Console.WriteLine(Char.IsPunctuation(ch)); // Output: "True" Console.WriteLine(Char.IsPunctuation("no punctuation", 3)); // Output: "False" } }
c
using System; public class IsSeparatorSample { public static void Main() { string str = "twain1 twain2"; Console.WriteLine(Char.IsSeparator('a')); // Output: "False" Console.WriteLine(Char.IsSeparator(str, 6)); // Output: "True" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid separator characters are members of the following categories in UnicodeCategory: SpaceSeparator, LineSeparator, or ParagraphSeparator.
using System; public class IsSeparatorSample { public static void Main() { string str = "twain1 twain2"; Console.WriteLine(Char.IsSeparator('a')); // Output: "False" Console.WriteLine(Char.IsSeparator(str, 6)); // Output: "True" } }
c
For more information about surrogate pairs, see the Unicode Standard at http://www.unicode.org.
using System; public class IsSurrogateSample { public static void Main() { string str = "\U00010F00"; // Unicode values between 0x10000 and 0x10FFF are represented by two 16-bit "surrogate" characters Console.WriteLine(Char.IsSurrogate('a')); // Output: "False" Console.WriteLine(Char.IsSurrogate(str, 0)); // Output: "True" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid surrogate characters are members of the following category in UnicodeCategory: Surrogate.
For more information about surrogate pairs, see the Unicode Standard at http://www.unicode.org.
using System; public class IsSurrogateSample { public static void Main() { string str = "\U00010F00"; // Unicode values between 0x10000 and 0x10FFF are represented by two 16-bit "surrogate" characters Console.WriteLine(Char.IsSurrogate('a')); // Output: "False" Console.WriteLine(Char.IsSurrogate(str, 0)); // Output: "True" } }
c
using System; public class IsSymbolSample { public static void Main() { string str = "non-symbolic characters"; Console.WriteLine(Char.IsSymbol('+')); // Output: "True" Console.WriteLine(Char.IsSymbol(str, 8)); // Output: "False" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid symbols are members of the following categories in UnicodeCategory: MathSymbol, CurrencySymbol, ModifierSymbol, and OtherSymbol.
using System; public class IsSymbolSample { public static void Main() { string str = "non-symbolic characters"; Console.WriteLine(Char.IsSymbol('+')); // Output: "True" Console.WriteLine(Char.IsSymbol(str, 8)); // Output: "False" } }
c
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid uppercase letters are members of the following category in UnicodeCategory: UppercaseLetter.
c
using System; public class IsWhiteSpaceSample { public static void Main() { string str = "black matter"; Console.WriteLine(Char.IsWhiteSpace('A')); // Output: "False" Console.WriteLine(Char.IsWhiteSpace(str, 5)); // Output: "True" } }
s
index
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
Valid white space characters are members of the SpaceSeparator category in UnicodeCategory, as well as these Unicode characters: hexadecimal 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x0085, 0x2028, and 0x2029.
using System; public class IsWhiteSpaceSample { public static void Main() { string str = "black matter"; Console.WriteLine(Char.IsWhiteSpace('A')); // Output: "False" Console.WriteLine(Char.IsWhiteSpace(str, 5)); // Output: "True" } }
protected object MemberwiseClone(); |
s
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
FormatException | s contains more than one character. |
using System; public class ParseSample { public static void Main() { Console.WriteLine(Char.Parse("A")); // Output: 'A' } }
c
-or-
The unchanged value of c, if c is already lowercase or not alphabetic.
Use String.ToLower to convert a string to lowercase.
using System; using System.Globalization; // for CultureInfo public class ToLowerSample { public static void Main() { Console.WriteLine(Char.ToLower('A')); // Output: "a" } }
public static char ToLower( |
c
culture
-or-
The unchanged value of c, if c is already lowercase or not alphabetic.
Exception Type | Condition |
---|---|
ArgumentNullException | culture is null. |
using System; using System.Globalization; // for CultureInfo public class ToLowerSample { public static void Main() { Console.WriteLine(Char.ToLower('A')); // Output: "a" } }
public override string ToString(); |
using System; public class ToStringSample { public static void Main() { char ch = 'a'; Console.WriteLine(ch.ToString()); // Output: "a" Console.WriteLine(Char.ToString('b')); // Output: "b" } }
c
using System; public class ToStringSample { public static void Main() { char ch = 'a'; Console.WriteLine(ch.ToString()); // Output: "a" Console.WriteLine(Char.ToString('b')); // Output: "b" } }
public string ToString( |
provider
using System; public class ToStringSample { public static void Main() { char ch = 'a'; Console.WriteLine(ch.ToString()); // Output: "a" Console.WriteLine(Char.ToString('b')); // Output: "b" } }
c
-or-
The unchanged value of c, if c is already uppercase or not alphabetic.
Use String.ToUpper to convert a string to uppercase.
public static char ToUpper( |
c
culture
-or-
The unchanged value of c, if c is already uppercase or not alphabetic.
Exception Type | Condition |
---|---|
ArgumentNullException | culture is null. |