Check unicode type for character in CSharp

Description

The following code shows how to check unicode type for character.

Example


 /* w  w  w  .  j  ava  2  s.c  o  m*/
    

using System;

class Sample 
{
    public static void Main() 
    {
        char cHigh = '\uD800';
        char cLow  = '\uDC00';
        string s1  = new String(new char[] {'a', '\uD800', '\uDC00', 'z'});
        Console.WriteLine("cHigh: {0:X4}", (int)cHigh);
        Console.WriteLine("cLow:  {0:X4}", (int)cLow);
        for(int i = 0; i < s1.Length; i++){
            Console.WriteLine("{0:X4} ", (int)s1[i]);
        }
        Console.WriteLine("cLow?  - {0}", Char.IsHighSurrogate(cLow));
        Console.WriteLine("cHigh? - {0}", Char.IsHighSurrogate(cHigh));
        Console.WriteLine("s1[0]? - {0}", Char.IsHighSurrogate(s1, 0));
        Console.WriteLine("s1[1]? - {0}", Char.IsHighSurrogate(s1, 1));
    
        Console.WriteLine("cLow?  - {0}", Char.IsLowSurrogate(cLow));
        Console.WriteLine("cHigh? - {0}", Char.IsLowSurrogate(cHigh));
        Console.WriteLine("s1[0]? - {0}", Char.IsLowSurrogate(s1, 0));
        Console.WriteLine("s1[2]? - {0}", Char.IsLowSurrogate(s1, 2));
    
        Console.WriteLine("cHigh and cLow?  - {0}", Char.IsSurrogatePair(cHigh, cLow));
        Console.WriteLine("s1[0] and s1[1]? - {0}", Char.IsSurrogatePair(s1, 0));
        Console.WriteLine("s1[1] and s1[2]? - {0}", Char.IsSurrogatePair(s1, 1));
        Console.WriteLine("s1[2] and s1[3]? - {0}", Char.IsSurrogatePair(s1, 2));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var