Csharp/C Sharp by API/System/Char

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

Char.IsControl

  
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
        static void Main(string[] args){
            Console.WriteLine(GetCharKind("f"));
            Console.WriteLine(GetCharKind("0"));
            Console.WriteLine(GetCharKind("."));
            Console.WriteLine(GetCharKind("}"));
        }
    public static String GetCharKind(char theChar)
    {
      if (Char.IsControl(theChar))
      {
        return "Control";
      }
      else if (Char.IsDigit(theChar))
      {
        return "Digit";
      }
      else if (Char.IsLetter(theChar))
      {
        return "Letter";
      }
      else if (Char.IsNumber(theChar))
      {
        return "Number";
      }
      else if (Char.IsPunctuation(theChar))
      {
        return "Punctuation";
      }
      else if (Char.IsSeparator(theChar))
      {
        return "Separator";
      }
      else if (Char.IsSurrogate(theChar))
      {
        return "Surrogate";
      }
      else if (Char.IsSymbol(theChar))
      {
        return "Symbol";
      }
      else if (Char.IsWhiteSpace(theChar))
      {
        return "Whitespace";
      }
      else
      {
        return "Unknown";
      }
    }
}


Char.IsDigit

  
using System;  
  
public class CharDemo {     
  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"); 
      if(Char.IsLetter(str[i])) 
        Console.Write(" letter"); 
      if(Char.IsLower(str[i])) 
        Console.Write(" lowercase"); 
      if(Char.IsUpper(str[i])) 
        Console.Write(" uppercase"); 
      if(Char.IsSymbol(str[i])) 
        Console.Write(" symbol"); 
      if(Char.IsSeparator(str[i])) 
        Console.Write(" separator"); 
      if(Char.IsWhiteSpace(str[i])) 
        Console.Write(" whitespace"); 
      if(Char.IsPunctuation(str[i])) 
        Console.Write(" punctuation"); 
 
      Console.WriteLine(); 
    } 
 
    Console.WriteLine("Original: " + str); 
 
    // Convert to upper case.    
    string newstr = ""; 
    for(i=0; i < str.Length; i++) 
      newstr += Char.ToUpper(str[i]); 
  
    Console.WriteLine("Uppercased: " + newstr); 
 
  }     
}


Char.IsLetter

 
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(); 
    } 
 
  }     
}


Char.IsLower

 

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(); 
    } 
 
  }     
}


Char.IsPunctuation

 
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(); 
    } 
  }
}


Char.IsSeparator

 

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(); 
    } 
 
  }     
}


Char.IsSymbol

 
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(); 
    } 
 
  }     
}


Char.IsUpper

 
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(); 
    } 
 
  }     
}


Char.IsWhiteSpace

 
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(); 
    } 
 
  }     
}


Char.MaxValue

  

using System;
class Test {
  public static void Main() {
       // First, print out the minimum values
       Console.WriteLine("System Minimums\n");
       Console.WriteLine( "MinSByte {0}", System.SByte.MinValue);
       Console.WriteLine( "MinByte {0}", System.Byte.MinValue);
       Console.WriteLine( "MinInt16 {0}", System.Int16.MinValue);
       Console.WriteLine( "MinUInt16 {0}", System.UInt16.MinValue);
       Console.WriteLine( "MinInt32 {0}", System.Int32.MinValue);
       Console.WriteLine( "MinUInt32 {0}", System.UInt32.MinValue);
       Console.WriteLine( "MinInt64 {0}", System.Int64.MinValue);
       Console.WriteLine( "MinUInt64 {0}", System.UInt64.MinValue);
       Console.WriteLine( "MinChar {0}", System.Char.MinValue);
       Console.WriteLine( "MinSingle {0}", System.Single.MinValue);
       Console.WriteLine( "MinDouble {0}", System.Double.MinValue);
       // Console.WriteLine( "MinBoolean {0}", System.Boolean.MinValue);
       Console.WriteLine( "MinDecimal {0}", System.Decimal.MinValue);
    
       Console.WriteLine("\nSystem Maximums\n");
       Console.WriteLine( "MaxSByte {0}", System.SByte.MaxValue);
       Console.WriteLine( "MaxByte {0}", System.Byte.MaxValue);
       Console.WriteLine( "MaxInt16 {0}", System.Int16.MaxValue);
       Console.WriteLine( "MaxUInt16 {0}", System.UInt16.MaxValue);
       Console.WriteLine( "MaxInt32 {0}", System.Int32.MaxValue);
       Console.WriteLine( "MaxUInt32 {0}", System.UInt32.MaxValue);
       Console.WriteLine( "MaxInt64 {0}", System.Int64.MaxValue);
       Console.WriteLine( "MaxUInt64 {0}", System.UInt64.MaxValue);
       Console.WriteLine( "MaxChar {0}", System.Char.MaxValue);
       Console.WriteLine( "MaxSingle {0}", System.Single.MaxValue);
       Console.WriteLine( "MaxDouble {0}", System.Double.MaxValue);
       Console.WriteLine( "MaxDecimal {0}", System.Decimal.MaxValue);
  }
}


Char.MinValue

  

using System;
class Test {
  public static void Main() {
       // First, print out the minimum values
       Console.WriteLine("System Minimums\n");
       Console.WriteLine( "MinSByte {0}", System.SByte.MinValue);
       Console.WriteLine( "MinByte {0}", System.Byte.MinValue);
       Console.WriteLine( "MinInt16 {0}", System.Int16.MinValue);
       Console.WriteLine( "MinUInt16 {0}", System.UInt16.MinValue);
       Console.WriteLine( "MinInt32 {0}", System.Int32.MinValue);
       Console.WriteLine( "MinUInt32 {0}", System.UInt32.MinValue);
       Console.WriteLine( "MinInt64 {0}", System.Int64.MinValue);
       Console.WriteLine( "MinUInt64 {0}", System.UInt64.MinValue);
       Console.WriteLine( "MinChar {0}", System.Char.MinValue);
       Console.WriteLine( "MinSingle {0}", System.Single.MinValue);
       Console.WriteLine( "MinDouble {0}", System.Double.MinValue);
       // Console.WriteLine( "MinBoolean {0}", System.Boolean.MinValue);
       Console.WriteLine( "MinDecimal {0}", System.Decimal.MinValue);
    
       Console.WriteLine("\nSystem Maximums\n");
       Console.WriteLine( "MaxSByte {0}", System.SByte.MaxValue);
       Console.WriteLine( "MaxByte {0}", System.Byte.MaxValue);
       Console.WriteLine( "MaxInt16 {0}", System.Int16.MaxValue);
       Console.WriteLine( "MaxUInt16 {0}", System.UInt16.MaxValue);
       Console.WriteLine( "MaxInt32 {0}", System.Int32.MaxValue);
       Console.WriteLine( "MaxUInt32 {0}", System.UInt32.MaxValue);
       Console.WriteLine( "MaxInt64 {0}", System.Int64.MaxValue);
       Console.WriteLine( "MaxUInt64 {0}", System.UInt64.MaxValue);
       Console.WriteLine( "MaxChar {0}", System.Char.MaxValue);
       Console.WriteLine( "MaxSingle {0}", System.Single.MaxValue);
       Console.WriteLine( "MaxDouble {0}", System.Double.MaxValue);
       Console.WriteLine( "MaxDecimal {0}", System.Decimal.MaxValue);
  }
}


Char.Parse(String value)

   
using System;
class MainClass
{
    public static void Main(string[] args)
    {
    char myChar = char.Parse("w");
    Console.WriteLine("-> Value of myChar: {0}\n", myChar);
    }
}


Char.ToUpper

  
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;
class Class1{
        static void Main(string[] args){
            Console.WriteLine(IsInRangeCaseInsensitive("c", "a", "G"));
            Console.WriteLine(IsInRangeCaseInsensitive("c", "a", "c"));
            Console.WriteLine(IsInRangeCaseInsensitive("c", "g", "g"));
            Console.WriteLine(IsInRangeCaseInsensitive((char)32, "a", "b"));
        }
    public static bool IsInRangeCaseInsensitive(char testChar, char startOfRange, char endOfRange)
    {
      testChar = char.ToUpper(testChar);
      startOfRange = char.ToUpper(startOfRange);
      endOfRange = char.ToUpper(endOfRange);
      
      if (testChar >= startOfRange && testChar <= endOfRange)
      {
        // testChar is within the range
        return (true);
      }
      else
      {
        // testChar is NOT within the range 
        return (false);
      }
    }
}