เมธอด Char.IsSurrogate() ใน C# บ่งชี้ว่าอักขระที่ระบุมีหน่วยรหัสเซอโรเกท (surrogate code unit) หรือไม่
ไวยากรณ์
public static bool IsSurrogate (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.IsSurrogate(str, 4);
if (res)
Console.WriteLine("Contains Surrogate value!");
else
Console.WriteLine("Does not contain Surrogate value!");
}
}ผลลัพธ์
Contains Surrogate value!
ตัวอย่างที่ 2 (ไม่ใช่ surrogate)
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("Contains Surrogate value!");
else
Console.WriteLine("Does not contain Surrogate value!");
}
}ผลลัพธ์
Does not contain Surrogate value!