Csharp/CSharp Tutorial/Data Type/char

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Assign value to a char type

  1. C# uses a 16-bit character type called Unicode.
  2. Unicode defines a character set that is large enough to represent all of the characters found in all human languages.
  3. There are no automatic type conversions from integer to char.

Char supplies methods that allow you to process and categorize characters.

Char defines the following fields:

  1. public const char MaxValue
  2. public const char MinValue

These represent the largest and smallest values that a char variable can hold.

Char implements the following interfaces: IComparable and IConvertible.


<source lang="csharp">using System; class MainClass {

 static void Main(string[] args)
 {
   char MyChar = "A";
       Console.WriteLine(MyChar);
 }

}</source>

A

Character Escape Sequences

Escape Sequence Description \a Alert (bell) \b Backspace \f Form feed \n New line (linefeed) \r Carriage return \t Horizontal tab \v Vertical tab \0 Null \" Single quote \" Double quote \\ Backslash

Char: IsDigit

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsDigit(str[i])) 
       Console.Write(" digit"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is
h is
i is
s is
  is
i is
s is
  is
a is
  is
t is
e is
s is
t is
. is
  is
$ is
2 is digit
3 is digit

Char: IsLetter

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsLetter(str[i])) 
       Console.Write(" letter"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is letter
h is letter
i is letter
s is letter
  is
i is letter
s is letter
  is
a is letter
  is
t is letter
e is letter
s is letter
t is letter
. is
  is
$ is
2 is
3 is

Char: IsLower/IsUpper

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsLower(str[i])) 
       Console.Write(" lowercase"); 
     if(Char.IsUpper(str[i])) 
       Console.Write(" uppercase"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is uppercase
h is lowercase
i is lowercase
s is lowercase
  is
i is lowercase
s is lowercase
  is
a is lowercase
  is
t is lowercase
e is lowercase
s is lowercase
t is lowercase
. is
  is
$ is
2 is
3 is

Char: IsPunctuation

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsPunctuation(str[i])) 
       Console.Write(" punctuation"); 

     Console.WriteLine(); 
   } 
 }

}</source>

T is
h is
i is
s is
  is
i is
s is
  is
a is
  is
t is
e is
s is
t is
. is punctuation
  is
$ is
2 is
3 is

Char: IsSeparator

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsSeparator(str[i])) 
       Console.Write(" separator"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is
h is
i is
s is
  is separator
i is
s is
  is separator
a is
  is separator
t is
e is
s is
t is
. is
  is separator
$ is
2 is
3 is

Char: IsSymbol

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsSymbol(str[i])) 
       Console.Write(" symbol"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is
h is
i is
s is
  is
i is
s is
  is
a is
  is
t is
e is
s is
t is
. is
  is
$ is symbol
2 is
3 is

Char: IsWhiteSpace

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 
   int i; 

   for(i=0; i < str.Length; i++) { 
     Console.Write(str[i] + " is"); 
     if(Char.IsWhiteSpace(str[i])) 
       Console.Write(" whitespace"); 

     Console.WriteLine(); 
   } 

 }     

}</source>

T is
h is
i is
s is
  is whitespace
i is
s is
  is whitespace
a is
  is whitespace
t is
e is
s is
t is
. is
  is whitespace
$ is
2 is
3 is

Char type Functionality

<source lang="csharp">using System; using System.Collections.Generic; using System.Text;

 class Program
 {
   static void Main(string[] args)
   {
     char myChar = "a";
     Console.WriteLine("char.IsDigit("a"): {0}", char.IsDigit(myChar));
     Console.WriteLine("char.IsLetter("a"): {0}", char.IsLetter(myChar));
     Console.WriteLine("char.IsWhiteSpace("Hello There", 5): {0}",char.IsWhiteSpace("Hello There", 5));
     Console.WriteLine("char.IsWhiteSpace("Hello There", 6): {0}",char.IsWhiteSpace("Hello There", 6));
     Console.WriteLine("char.IsPunctuation("?"): {0}",char.IsPunctuation("?"));
   }
 }</source>

Convert character to upper case

<source lang="csharp">using System;

class MainClass {

 public static void Main() {     
   string str = "This is a test. $23"; 

   Console.WriteLine("Original: " + str); 

   // .    
   string newstr = ""; 
   for(int i=0; i < str.Length; i++) 
     newstr += Char.ToUpper(str[i]); 
 
   Console.WriteLine("Uppercased: " + newstr); 
 }     

}</source>

Original: This is a test. $23
Uppercased: THIS IS A TEST. $23

Determining the Character Difference between Two Characters

<source lang="csharp">class MainClass {

 static void Main()
 {
   int distance = "f" - "c";
   System.Console.WriteLine(distance);
 }

}</source>

Displaying Each Character"s ASCII Value in Descending Order

<source lang="csharp">class MainClass {

 static void Main()
 {
   char current;
   int asciiValue;
   
   current="z";
   do
   {
     asciiValue = current;
     System.Console.Write("{0}={1}\t", current, asciiValue);
     current--;
   
   }
   while(current>="a");
 }

}</source>

Get characters in a string

<source lang="csharp">using System; class Test{

   public static void Main()
   {
       string s = "Test String";
       
       for (int index = 0; index < s.Length; index++)
          Console.WriteLine("Char: {0}", s[index]);
   }

}</source>

Char: T
Char: e
Char: s
Char: t
Char:
Char: S
Char: t
Char: r
Char: i
Char: n
Char: g

Methods Defined by Char

Method Meaning public int CompareTo(object v) Returns zero if the characters are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. public override bool Equals(object v) Returns true if the value of the invoking object equals the value of v. public override int GetHashCode() Returns the hash code for the invoking object. public static double GetNumericValue(char ch) Returns the numeric value of ch if ch is a digit. Otherwise, returns -1. public static doubleGetNumericValue(string str, int idx) Returns the numeric value of str[idx] if that character is a digit. Otherwise, returns -1. public TypeCode GetTypeCode() Returns the TypeCode enumeration value for Char, which is TypeCode.Char. public static UnicodeCategoryGetUnicodeCategory(char ch) Returns the UnicodeCategory enumeration value for ch. UnicodeCategory is an enumeration defined by System.Globalization that categorizes Unicode characters. public static UnicodeCategoryGetUnicodeCategory(string str, int idx) Returns the UnicodeCategory enumeration value for str[idx]. UnicodeCategory is an enumeration defined by System.Globalization that categorizes Unicode characters. public static bool IsControl(char ch) Is ch a control character. public static bool IsControl(string str, int idx) Is str[idx] a control character. public static bool IsDigit(char ch) Is ch a digit. public static bool IsDigit(string str, int idx) Is str[idx] a digit. public static bool IsLetter(char ch) Is ch a letter of the alphabet. public static bool IsLetter(string str, int idx) Is str[idx] a letter of the alphabet. public static bool IsLetterOrDigit(char ch) Is ch either a letter of the alphabet or a digit. public static boolIsLetterOrDigit(string str, int idx) Is str[idx] either a letter of the alphabet or a digit. public static bool IsLower(char ch) Is ch a lowercase letter of the alphabet. public static bool IsLower(string str, int idx) Is str[idx] a lowercase of the alphabet. public static bool IsNumber(char ch) Is ch a hexadecimal digit, which is 0 through 9 or A through F. public static bool IsNumber(string str, int idx) Is str[idx] a hexadecimal digit, which is 0 through 9 or A through F. public static bool IsPunctuation(char ch) Returns true if ch is a punctuation character. Otherwise, returns false. public static boolIsPunctuation(string str, int idx) Is str[idx] a punctuation character. public static bool IsSeparator(char ch) Is ch a separator character, such as a space. public static bool IsSeparator(string str, int idx) Is str[idx] a separator character, such as a space. public static bool IsSurrogate(char ch) Is ch a Unicode surrogate character. public static bool IsSurrogate(string str, int idx) Is str[idx] a Unicode surrogate character. public static bool IsSymbol(char ch) Is ch a symbolic character, such as the currency symbol. public static bool IsSymbol(string str, int idx) Is str[idx] a symbolic character, such as the currency symbol. public static bool IsUpper(char ch) Is ch an uppercase letter. public static bool IsUpper(string str, int idx) Is str[idx] an uppercase letter. public static bool IsWhiteSpace(char ch) Is ch a whitespace character, such as a space or tab. public static boolIsWhiteSpace(string str, int idx) Is str[idx] a whitespace character, such as a space or tab. public static char Parse(string str) Parse the string. If str contains more than one character, a FormatException is thrown. public static char ToLower(char ch) Returns the lowercase equivalent of ch if ch is an uppercase letter. Otherwise, ch is returned unchanged. public static charToLower(char ch, CultureInfo c) Returns the lowercase equivalent of ch if ch is an uppercase letter. Otherwise, ch is returned unchanged. The conversion is handled in accordance with the specified cultural information. CultureInfo is a class defined in System.Globalization. public static char ToUpper(char ch) Returns the uppercase equivalent of ch if ch is a lowercase letter. Otherwise, ch is returned unchanged. public static char ToUpper(char ch,CultureInfo c) Returns the uppercase equivalent of ch if ch is a lowercase letter. Otherwise, ch is returned unchanged. The conversion is handled in accordance with the specified cultural information. CultureInfo is a class defined in System.Globalization. public override string ToString() Returns the string representation of the value of the invoking Char. public static string ToString(char ch) Returns the string representation of ch. public string ToString(IFormatProvider fmtpvdr) Returns the string representation of the invoking Char using the specified culture information.

Use a char to control the switch.

<source lang="csharp">using System;

class MainClass {

 public static void Main() { 
   char ch; 

   for(ch="A"; ch<= "E"; ch++) 
     switch(ch) { 
       case "A":  
         Console.WriteLine("ch is A"); 
         break; 
       case "B":  
         Console.WriteLine("ch is B"); 
         break; 
       case "C":  
         Console.WriteLine("ch is C"); 
         break; 
       case "D":  
         Console.WriteLine("ch is D"); 
         break; 
       case "E":  
         Console.WriteLine("ch is E"); 
         break; 
     }     
 } 

}</source>

ch is A
ch is B
ch is C
ch is D
ch is E

Using the Plus Operator with the char Data Type

<source lang="csharp">class MainClass {

 static void Main()
 {
   int n = "3" + "4";
   char c = (char)n;
   System.Console.WriteLine(c);  // Writes out g.
 }

}</source>

Using Unicode Encoding to Display a Smiley Face

<source lang="csharp">class MainClass {

 static void Main()
 {
   System.Console.Write("\u003A");
   System.Console.WriteLine("\u0029");
 }

}</source>