C#. Data type char. Structure System.Char. Methods




Data type char. Structure System.Char. Methods


Contents


Search other websites:

1. Features of the char data type. The internal representation. The structure System.Char

The .NET Framework char data type is introduced to represent a single character. Each character is encoded in Unicode format. The Unicode format uses the UTF-16 code form, in which each character is represented by a 16-bit numeric positive value.

Bit depth of 16 bits (rather than 8 bits, as it was before) is explained by the fact that it is necessary to ensure the representation of wide character sets in some languages. For example, in Chinese, the number of characters used cannot be placed in the number of possible values that an 8-bit representation of a character gives.

In C #, each 16-bit value is represented by a range from 0x0000 to 0xFFFF and is placed in the System.Char structure.

The System.Char structure represents methods that are grouped by the following criteria:

  • comparing objects of type char;
  • converting the current char-object to an object of another type;
  • character case conversion;
  • category definition of the current character.

 

2. A variable declaration of type char. Examples of char constants

The char data type can represent different kinds of characters:

  • characters that are displayed on the keyboard;
  • special control characters such as ‘\n’, ‘\b’, ‘\t’ and others.

The following are examples of declaring a variable of type char and using some character constants

char c1; // Declaring a variable of type char
char c2 = '\x173'; // Declaring with initialization

c1 = ' '; // space character
c1 = '\x20'; // space character in the form of code in the 16th number system

c2 = '\n'; // newline character
c2 = 'Z'; // uppercase letter Z
c2 = 'z'; // lowercase letter z

 

3. How to determine code of character. Example

To determine the chararacter code, you need to implement an explicit cast operation, as shown below.

char c = '\x20'; // Declaration with initialization
uint code;
code = (uint)c; // code = 32 (in decimal)

c = '\n';
code = (uint)c; // code = 10

c = 'D';
code = (uint)c; // code = 68

 

4. Examples of methods that are implemented in a structure of type Char

The char type is an alias of the System.Char type structure, which has a number of methods. The main ones are listed below.

4.1. Method IsControl(). Determining if a character is a control character

The IsControl() method determines whether a character belongs to the category of control characters according to Unicode encoding. Control characters include ‘\n’, ‘\t’, ‘\b’ and others. The method returns a value of type bool and has two overloaded implementations.

The general form of the first implementation of the method is as follows:

bool char.IsControl(char c);

here c – verifiable character. If c is the control character, then the function returns true.

The general form of the second implementation of the method is as follows:

bool char.IsControl(string s, int index);

here

  • s – string in which the check occurs;
  • index – the position of the character in string s for which the check is performed. If c is the control character, then the function returns true.

Example.

// Determine if there is a symbol of control category
bool fRes;
fRes = char.IsControl('.'); // fRes = false
fRes = char.IsControl('\t'); // fRes = true
fRes = char.IsControl('\\'); // fRes = false
fRes = char.IsControl('\n'); // fRes = true

// Is there a character at the given position of the given string by the control
fRes = char.IsControl("abcd\nghi", 4); // fRes = true
fRes = char.IsControl("abcd\nghi", 3); // fRes = false

 

4.2. Method IsDigit(). Determining if a character is a number

The IsDigit() method is designed to determine if a character is a digit. The method has two overloaded implementations:

bool char.IsDigit(char c);
bool char.IsDigit(string s, int index);

here

  • c – character to check;
  • s – the string in which the character at the index position is checked. The specified character is denoted by s[index].

If the specified character is a digit, then the function returns true.

Example.

// 2. Determine if a character is a number
bool fRes;
char c = '7';
fRes = System.Char.IsDigit(c); // fRes = true
fRes = char.IsDigit('a'); // fRes = false
fRes = char.IsDigit('.'); // fRes = false
fRes = char.IsDigit("550", 0); // fRes = true
fRes = char.IsDigit("A50", 0); // fRes = false

 

4.3. Method IsLetter(). Determining if a character is alphabetic

The IsLetter() method returns true if the specified character is alphabetic. The symbol is specified by one of two overloaded implementations of the method

bool char.IsLetter(char c);
bool char.IsLetter(string s, int index);

Example.

// Determine if a character is alphabetic
bool fRes;
fRes = char.IsLetter('z'); // fRes = true
fRes = char.IsLetter(';'); // fRes = false
fRes = System.Char.IsLetter('\n'); // fRes = false
fRes = char.IsLetter("\na+-8", 1); // fRes = true
fRes = char.IsLetter("\na+-8", 2); // fRes = false

 

4.4. Method IsLower(). Determining if a character is lowercase

The IsLower() method returns true if the specified character is lowercase. The method has two overloaded implementations

bool char.IsLower(char c);
bool char.IsLower(string s, int index);

here

  • c – character to check;
  • s – the string in which the character at the index position is defined.

Example.

// Determine if a character is lowercase
bool fRes;
fRes = char.IsLower('Z'); // fRes = false
fRes = char.IsLower(';'); // fRes = false
fRes = char.IsLower("hELLO wORLD!", 0); // fRes = true
fRes = char.IsLower("ABCD", 2); // fRes = false

 

4.5. Method IsUpper(). Determining if a character is uppercase

The IsUpper() method is used to determine whether a character is in uppercase. In this case, the method returns true. The method has two overloaded implementations

bool char.IsUpper(char c);
bool char.IsUpper(string s, int index);

here

  • c – character to check;
  • s – the string in which the character at the index position is checked. The character to be checked has a value of s[index].

Example.

// Determine if a character is uppercase
bool fRes;
fRes = char.IsUpper('Ю'); // fRes = true, cyrillic characters are also processed.
fRes = char.IsUpper('ю'); // fRes = false

fRes = char.IsUpper("abc+DEF", 4); // fRes = true

 

4.6. Method IsNumber(). Determining if a character is a number

The IsNumber() method determines whether a character is a number. If the character is a number, the method returns true. The method has two overloaded implementations:

bool char.IsNumber(char c);
bool char.IsNumber(string s, int index);

here

  • c – character to check;
  • s – string containing the character to be checked. The character position is specified by the index parameter.

Example.

// Determine if a character is a number
bool fRes;
fRes = char.IsNumber('8'); // fRes = true
fRes = char.IsNumber('\t'); // fRes = false

fRes = char.IsNumber("2+2=5", 2); // fRes = true
fRes = char.IsNumber("2+2=4", 3); // fRes = false

 

4.7. Method IsSeparator(). Determines if the character is a separator

The IsSeparator() method is used to determine whether a character belongs to the separator category according to Unicode encoding. If the character is a separator, then the method returns true. The method has two overloaded implementations:

bool char.IsSeparator(char c);
bool char.IsSeparator(string s, int index);

here

  • c – character to check;
  • s – a string in which at the index position the character to be checked is obtained.

Example.

// Determine if a character is a separator
fRes = char.IsSeparator(' '); // fRes = true
fRes = char.IsSeparator(','); // fRes = false
fRes = char.IsSeparator('\n'); // fRes = false
fRes = char.IsSeparator('\t'); // fRes = false

 

4.8. Method IsWhiteSpace(). Determines if a character is a space

The IsWhiteSpace() method determines whether a character belongs to the category of whitespace according to Unicode encoding. White space characters include a space, a tab character, a newline character, etc. If successful, the method returns true.

There are two implementations of the method:

bool char.IsWhiteSpase(char c);
bool char.IsWhiteSpase(string s, int index);

here

  • c – character being checked;
  • s – a string in which the character to be checked is determined at the index position. The value of a character is defined as s[index].

Example.

// Determine if a character is a space
bool fRes;
fRes = char.IsWhiteSpace('\t'); // fRes = true
fRes = char.IsWhiteSpace('\n'); // fRes = true
fRes = char.IsWhiteSpace(' '); // fRes = true
fRes = char.IsWhiteSpace('\b'); // fRes = false
fRes = char.IsWhiteSpace(';'); // fRes = false

 

4.9. Method ToLower(). Converts a character to lowercase

The ToLower() method is used to translate a character to lowercase (if possible). The method returns the converted character. According to the .NET Framework documentation, the method has two overloaded implementations

char char.ToLower(char c);
char char.ToLower(char c, System.Globalization.CultureInfo culture);

here

  • c – a character that is lowercase
  • culture – culture information which is located in the System.Globalization.Culture class. Culture describes the names for the culture, the writing system, the calendar used, sort order, and formatting for strings and dates.

Example.

// Convert character to lowercase
char c1, c2;
c2 = 'A';
c1 = char.ToLower(c2); // c1 = 'a'
c1 = char.ToLower(c2, CultureInfo.CurrentCulture); // c1 = 'a'
c1 = char.ToLower(c2, CultureInfo.InvariantCulture); // c1 = 'a'
c1 = char.ToLower('+'); // c1 = '+'

 

4.10. Method ToUpper(). Converts a character to uppercase

The ToUpper() method is used to translate the specified character in upper case (if possible). If conversion is possible (a character has a two-case representation), the method returns a uppercase character. If conversion is not possible, the value of the original character is returned. According to the documentation of the .NET Framework, the method has two overloaded implementations:

char char.ToUpper(char c);
char char.ToUpper(char c,

here

  • c – character, which translates to lower case;
  • culture – Information about the culture, which is located in System.Globalization.Culture class. Culture describes the names for the culture, the recording system, the calendar used, sort order, and formatting for strings and dates.

Example.

// Converting the character to uppercase
char c1, c2;
c2 = 'k';
c1 = char.ToUpper(c2); // c1 = 'K'
c1 = char.ToUpper('f'); // c1 = 'F'
c1 = char.ToUpper('='); // c1 = '='

 

5. Constants MinValue, MaxValue

Two constants are introduced in the System.Char structure:

  • MinValue – represents the minimum char value that is equal to ‘\0’;
  • MaxValue – represents the maximum allowed value of type char, which is equal to ‘\uffff’.

An appeal to each constant in the program can be like this

char.MinValue
char.MaxValue

 


Related topics

 


 

0
fb-share-icon20