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

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

Phương thức Char.IsSurrogate() trong C# chỉ ra xem ký tự chỉ định có đơn vị mã surrogate hay không.

Cú pháp

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

Tham số str là chuỗi, index là vị trí 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.IsSurrogate(str, 4); 
        if (res) 
            Console.WriteLine("Chứa giá trị Surrogate!"); 
        else 
            Console.WriteLine("Không chứa giá trị Surrogate!"); 
    } 
}

Kết quả

Chứa giá trị Surrogate!

Ví dụ khác

using System; 
public class Demo { 
    public static void Main(){ 
        string str = new String(new char[] { 'k', 'm', 'g', 't', 'w' }); 
        bool res = Char.IsSurrogate(str, 4); 
        if (res) 
            Console.WriteLine("Chứa giá trị Surrogate!"); 
        else 
            Console.WriteLine("Không chứa giá trị Surrogate!"); 
    } 
}

Kết quả

Không chứa giá trị Surrogate!