Export (0) Print
Expand All

Char.IsSurrogatePair Method (String, Int32)

Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public:
static bool IsSurrogatePair (
	String^ s, 
	int index
)
public static boolean IsSurrogatePair (
	String s, 
	int index
)
public static function IsSurrogatePair (
	s : String, 
	index : int
) : boolean
Not applicable.

Parameters

s

A string.

index

A position within s.

Return Value

true if the s parameter and the index parameter specify a pair of adjacent characters, and the numeric value of the character at position index ranges from U+D800 through U+DBFF, and the numeric value of the character at position index+1 ranges from U+DC00 through U+DFFF; otherwise, false.

Exception typeCondition

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is not a position within s.

Ordinarily, a single character is represented by a single 16-bit Unicode code point. UTF-16 encoding also supports surrogate pairs, which allow a single abstract character to be represented by two 16-bit code points. The first code point, whose value can range from U+D800 to U+DBFF, is the high surrogate. The second code point, whose value can range from U+DC00 to U+DFFF, is the low surrogate. Individual surrogate code points have no interpretation of their own. For more information about surrogates and the Unicode Standard, see the Unicode home page.

The following code example demonstrates the IsHighSurrogate, IsLowSurrogate, and IsSurrogatePair methods.

// This example demonstrates the Char.IsLowSurrogate() method
//                                    IsHighSurrogate() method
//                                    IsSurrogatePair() method
using namespace System;
int main()
{
   Char cHigh = L'\xD800';
   Char cLow = L'\xDC00';
   array<Char>^temp0 = {L'a',L'\xD800',L'\xDC00',L'z'};
   String^ s1 = gcnew String( temp0 );
   String^ divider = String::Concat( Environment::NewLine, gcnew String( '-',70 ), Environment::NewLine );
   Console::WriteLine();
   Console::WriteLine( "Hexadecimal code point of the character, cHigh: {0:X4}", (int)cHigh );
   Console::WriteLine( "Hexadecimal code point of the character, cLow:  {0:X4}", (int)cLow );
   Console::WriteLine();
   Console::WriteLine( "Characters in string, s1: 'a', high surrogate, low surrogate, 'z'" );
   Console::WriteLine( "Hexadecimal code points of the characters in string, s1: " );
   for ( int i = 0; i < s1->Length; i++ )
   {
      Console::WriteLine( "s1[{0}] = {1:X4} ", i, (int)s1[ i ] );
   }
   Console::WriteLine( divider );
   Console::WriteLine( "Is each of the following characters a high surrogate?" );
   Console::WriteLine( "A1) cLow?  - {0}", Char::IsHighSurrogate( cLow ) );
   Console::WriteLine( "A2) cHigh? - {0}", Char::IsHighSurrogate( cHigh ) );
   Console::WriteLine( "A3) s1[0]? - {0}", Char::IsHighSurrogate( s1, 0 ) );
   Console::WriteLine( "A4) s1[1]? - {0}", Char::IsHighSurrogate( s1, 1 ) );
   Console::WriteLine( divider );
   Console::WriteLine( "Is each of the following characters a low surrogate?" );
   Console::WriteLine( "B1) cLow?  - {0}", Char::IsLowSurrogate( cLow ) );
   Console::WriteLine( "B2) cHigh? - {0}", Char::IsLowSurrogate( cHigh ) );
   Console::WriteLine( "B3) s1[0]? - {0}", Char::IsLowSurrogate( s1, 0 ) );
   Console::WriteLine( "B4) s1[2]? - {0}", Char::IsLowSurrogate( s1, 2 ) );
   Console::WriteLine( divider );
   Console::WriteLine( "Is each of the following pairs of characters a surrogate pair?" );
   Console::WriteLine( "C1) cHigh and cLow?  - {0}", Char::IsSurrogatePair( cHigh, cLow ) );
   Console::WriteLine( "C2) s1[0] and s1[1]? - {0}", Char::IsSurrogatePair( s1, 0 ) );
   Console::WriteLine( "C3) s1[1] and s1[2]? - {0}", Char::IsSurrogatePair( s1, 1 ) );
   Console::WriteLine( "C4) s1[2] and s1[3]? - {0}", Char::IsSurrogatePair( s1, 2 ) );
   Console::WriteLine( divider );
}

/*
This example produces the following results:

Hexadecimal code point of the character, cHigh: D800
Hexadecimal code point of the character, cLow:  DC00

Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
Hexadecimal code points of the characters in string, s1:
s1[0] = 0061
s1[1] = D800
s1[2] = DC00
s1[3] = 007A

----------------------------------------------------------------------

Is each of the following characters a high surrogate?
A1) cLow?  - False
A2) cHigh? - True
A3) s1[0]? - False
A4) s1[1]? - True

----------------------------------------------------------------------

Is each of the following characters a low surrogate?
B1) cLow?  - True
B2) cHigh? - False
B3) s1[0]? - False
B4) s1[2]? - True

----------------------------------------------------------------------

Is each of the following pairs of characters a surrogate pair?
C1) cHigh and cLow?  - True
C2) s1[0] and s1[1]? - False
C3) s1[1] and s1[2]? - True
C4) s1[2] and s1[3]? - False

----------------------------------------------------------------------

*/

// This example demonstrates the Char.IsLowSurrogate() method
// IsHighSurrogate() method
// sIsSurrogatePair() method
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        char cHigh = '\uD800';
        char cLow = '\uDC00';
        String s1 = new String(new char[] { 'a', '\uD800', '\uDC00', 'z' });
        String divider = String.Concat(Environment.get_NewLine(), 
            new String('-', 70), Environment.get_NewLine());

        Console.WriteLine();
        Console.WriteLine("Hexadecimal code point of the character,"
            + " cHigh: {0:X4}", (Int32)cHigh);
        Console.WriteLine("Hexadecimal code point of the character,"
            + " cLow:  {0:X4}", (Int32)(cLow));
        Console.WriteLine();
        Console.WriteLine("Characters in string, s1: 'a', high surrogate,"
            + " low surrogate, 'z'");
        Console.WriteLine("Hexadecimal code points of the characters in string,"
            + " s1: ");
        for (int i = 0; i < s1.get_Length(); i++) {
            Console.WriteLine("s1[{0}] = {1:X4} ", (Int32)i, (Int32)s1.
                get_Chars(i));
        }
        Console.WriteLine(divider);
        Console.WriteLine("Is each of the following characters a high surrogate?");
        Console.WriteLine("A1) cLow?  - {0}", (System.Boolean)Char.
            IsHighSurrogate(cLow));
        Console.WriteLine("A2) cHigh? - {0}", (System.Boolean)Char.
            IsHighSurrogate(cHigh));
        Console.WriteLine("A3) s1[0]? - {0}", (System.Boolean)Char.
            IsHighSurrogate(s1, 0));
        Console.WriteLine("A4) s1[1]? - {0}", (System.Boolean)Char.
            IsHighSurrogate(s1, 1));
        Console.WriteLine(divider);

        Console.WriteLine("Is each of the following characters a low surrogate?");
        Console.WriteLine("B1) cLow?  - {0}", (System.Boolean)Char.
            IsLowSurrogate(cLow));
        Console.WriteLine("B2) cHigh? - {0}", (System.Boolean)Char.
            IsLowSurrogate(cHigh));
        Console.WriteLine("B3) s1[0]? - {0}", (System.Boolean)Char.
            IsLowSurrogate(s1, 0));
        Console.WriteLine("B4) s1[2]? - {0}", (System.Boolean)Char.
            IsLowSurrogate(s1, 2));
        Console.WriteLine(divider);

        Console.WriteLine("Is each of the following pairs of characters a"
            + " surrogate pair?");
        Console.WriteLine("C1) cHigh and cLow?  - {0}", (System.Boolean)Char.
            IsSurrogatePair(cHigh, cLow));
        Console.WriteLine("C2) s1[0] and s1[1]? - {0}", (System.Boolean)Char.
            IsSurrogatePair(s1, 0));
        Console.WriteLine("C3) s1[1] and s1[2]? - {0}", (System.Boolean)Char.
            IsSurrogatePair(s1, 1));
        Console.WriteLine("C4) s1[2] and s1[3]? - {0}", (System.Boolean)Char.
            IsSurrogatePair(s1, 2));
        Console.WriteLine(divider);
    } //main
} //Sample

/*
This example produces the following results:

Hexadecimal code point of the character, cHigh: D800
Hexadecimal code point of the character, cLow:  DC00

Characters in string, s1: 'a', high surrogate, low surrogate, 'z'
Hexadecimal code points of the characters in string, s1:
s1[0] = 0061
s1[1] = D800
s1[2] = DC00
s1[3] = 007A

----------------------------------------------------------------------

Is each of the following characters a high surrogate?
A1) cLow?  - False
A2) cHigh? - True
A3) s1[0]? - False
A4) s1[1]? - True

----------------------------------------------------------------------

Is each of the following characters a low surrogate?
B1) cLow?  - True
B2) cHigh? - False
B3) s1[0]? - False
B4) s1[2]? - True

----------------------------------------------------------------------

Is each of the following pairs of characters a surrogate pair?
C1) cHigh and cLow?  - True
C2) s1[0] and s1[1]? - False
C3) s1[1] and s1[2]? - True
C4) s1[2] and s1[3]? - False

----------------------------------------------------------------------

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft