欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

忽略emoji 显示其他字符

程序员文章站 2022-04-21 10:20:34
...

转载- 解决了项目中 微信名称 emoji 过滤的问题

链接附上:https://www.cnblogs.com/lilo202/p/8058012.html

谢谢 

赋值给pulic 打包出来手机测试就可以啦....

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class Emoji : MonoBehaviour {

    public UIInput Input_1;
    public UIButton btn;
    void Start()
    {
        UIEventListener.Get(btn.gameObject).onClick = Info;
    }

    public void Info(GameObject go)
    {
        Debug.LogError("Emoji_Content:"+ Input_1.value);
        Input_1.value = filterEmoji(Input_1.value);
    }


    #region 去掉表情符号
    /// <summary>
    /// 去掉表情符号
    /// </summary>
    /// <param name="codePoint"></param>
    /// <returns></returns>
    public static bool isEmojiCharacter(char codePoint)
    {
        return (codePoint >= 0x2600 && codePoint <= 0x27BF) // 杂项符号与符号字体
               || codePoint == 0x303D
               || codePoint == 0x2049
               || codePoint == 0x203C
               || (codePoint >= 0x2000 && codePoint <= 0x200F) //
               || (codePoint >= 0x2028 && codePoint <= 0x202F) //
               || codePoint == 0x205F //
               || (codePoint >= 0x2065 && codePoint <= 0x206F) //
                                                               /* 标点符号占用区域 */
               || (codePoint >= 0x2100 && codePoint <= 0x214F) // 字母符号
               || (codePoint >= 0x2300 && codePoint <= 0x23FF) // 各种技术符号
               || (codePoint >= 0x2B00 && codePoint <= 0x2BFF) // 箭头A
               || (codePoint >= 0x2900 && codePoint <= 0x297F) // 箭头B
               || (codePoint >= 0x3200 && codePoint <= 0x32FF) // 中文符号
               || (codePoint >= 0xD800 && codePoint <= 0xDFFF) // 高低位替代符保留区域
               || (codePoint >= 0xE000 && codePoint <= 0xF8FF) // 私有保留区域
               || (codePoint >= 0xFE00 && codePoint <= 0xFE0F) // 变异选择器
                                                               //   || (codePoint >= U + 2600 && codePoint <= 0xFE0F)
               || codePoint >= 0x10000; // Plane在第二平面以上的,char都不可以存,全部都转

    }
    /// <summary>
    /// 检测是否有emoji字符
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public static bool containsEmoji(String source)
    {
        if (string.IsNullOrEmpty(source))
        {
            return false;
        }

        int len = source.Length;

        for (int i = 0; i < len; i++)
        {
            char codePoint = source[i];

            if (isEmojiCharacter(codePoint))
            {
                //do nothing,判断到了这里表明,确认有表情字符
                return true;
            }
        }

        return false;
    }
    /// <summary>
    /// 过滤emoji 或者 其他非文字类型的字符
    /// </summary>
    /// <param name="source">param source</param>
    /// <returns></returns>
    public static String filterEmoji(String source)
    {
        if (string.IsNullOrEmpty(source))
        {
            return "";
        }
        source = source.Replace("[^\\u0000-\\uFFFF]", "").Replace("??", "");
        if (!containsEmoji(source))
        {
            return source; //如果不包含,直接返回
        }
        //到这里铁定包含
        StringBuilder buf = null;

        int len = source.Length;

        for (int i = 0; i < len; i++)
        {
            char codePoint = source[i];

            if (!isEmojiCharacter(codePoint))
            {
                if (buf == null)
                {
                    buf = new StringBuilder(source.Length);
                }

                buf.Append(codePoint);
            }
            else
            {
            }
        }

        if (buf == null)
        {
            return source; //如果没有找到 emoji表情,则返回源字符串
        }
        else
        {
            if (buf.Length == len)
            {
                //这里的意义在于尽可能少的toString,因为会重新生成字符串
                buf = null;
                return source;
            }
            else
            {
                return buf.ToString();
            }
        }

    }
    #endregion
}

 

相关标签: unity emoji