Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Char.IsSurrogatePair(String, Int32) trong C#

Phương thức Char.IsSurrogatePair() trong C# chỉ ra xem hai đối tượng Char liền kề tại vị trí chỉ định trong chuỗi có tạo thành một surrogate pair hay không.

Cú pháp

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

Tham số str là chuỗi, index là vị trí bắt đầu của cặp ký tự cần đánh giá trong str.

Ví dụ

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("Chứa Surrogate pair!"); 
        else 
            Console.WriteLine("Không chứa Surrogate pair!"); 
    } 
}

Kết quả

Không chứa Surrogate pair!

Ví dụ khác

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("Chứa Surrogate pair!"); 
        else 
            Console.WriteLine("Không chứa Surrogate pair!"); 
    } 
}

Kết quả

Chứa Surrogate pair!