判断字符串是否包含Emoji表情

public static class EmojiUtils

{
/// <summary>
/// 判断String中是否包含Emoji
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool ContainsEmoji(string source)
{
    int len = source.Length;
    foreach (char item in source)
    {
          if (!IsEmojiCharacter(item))
         {

           //如果不能匹配,则该字符是Emoji表情
           return true;
         }
    }
    return false;
}

 


/// <summary>
///根据char判断是否是Emoji
/// </summary>
/// <param name="codePoint"></param>
/// <returns></returns>
private static bool IsEmojiCharacter(char codePoint)
{
       return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                        || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                        || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                        || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}

}

posted on 2022-04-07 16:21  ausions  阅读(713)  评论(1编辑  收藏  举报