忽略emoji 显示其他字符

转载- 解决了项目中 微信名称 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
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值