如果我有一个字符串,例如
请注意,如评论所述,此方法无法处理家庭表情符号。它仅适用于字符数不超过2个字符的表情符号。示例的输出将为:
"123"
,我该如何将其拆分为数组,使其看起来像["", "1", "2", "3", ""]
?如果使用ToCharArray()
,第一个表情符号会被拆分为2个字符,第二个表情符号会被拆分为7个字符。
更新
现在解决方案如下:public static List<string> GetCharacters(string text)
{
char[] ca = text.ToCharArray();
List<string> characters = new List<string>();
for (int i = 0; i < ca.Length; i++)
{
char c = ca[i];
if (c > 65535) continue;
if (char.IsHighSurrogate(c))
{
i++;
characters.Add(new string(new[] { c, ca[i] }));
}
else
characters.Add(new string(new[] { c }));
}
return characters;
}
请注意,如评论所述,此方法无法处理家庭表情符号。它仅适用于字符数不超过2个字符的表情符号。示例的输出将为:
["", "1", "2", "3", "", "", "", ""]
。