Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Char.IsSurrogatePair(String, Int32) ใน C#


เมธอด Char.IsSurrogatePair() ใน C# ใช้ระบุว่าอ็อบเจ็กต์ Char สองตัวที่อยู่ติดกันที่ตำแหน่งที่ระบุในสตริงเป็นคู่เซอโรเกท (surrogate pair) หรือไม่

ไวยากรณ์

public static bool IsSurrogatePair (string str, int index);

str คือสตริง ส่วน index คือตำแหน่งเริ่มต้นของคู่อักขระที่ต้องการประเมินใน str

ตัวอย่าง (ไม่ใช่คู่ surrogate)

using System; 
public class Demo { 
    public static void Main(){ 
        string str = new String(new char[] { 'k', 'm', 'g', 't', '\uD800' }); 
        bool res = Char.IsSurrogatePair(str, 4); 
        if (res) 
            Console.WriteLine("Contains Surrogate pair!"); 
        else 
            Console.WriteLine("Does not contain Surrogate pair!"); 
    } 
}

ผลลัพธ์

Does not contain Surrogate pair!

ตัวอย่างที่ 2 (เป็นคู่ surrogate)

using System; 
public class Demo { 
    public static void Main(){ 
        string str = new String(new char[] { 'k', 'l', 'a', '\uD800', '\uDC00' }); 
        bool res = Char.IsSurrogatePair(str, 3); 
        if (res) 
            Console.WriteLine("Contains Surrogate pair!"); 
        else 
            Console.WriteLine("Does not contain Surrogate pair!"); 
    } 
}

ผลลัพธ์

Contains Surrogate pair!