runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  710 随笔 :: 0 文章 :: 127 评论 :: 98万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C#.NET FRAMEWORK .NET CORE .NET6 .NET8 判断是否Emoji

 

工具类:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleThreadTimer
{
    public class EmojiUtil
    {
        /// <summary>
        /// 判断String中是否包含Emoji
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool ContainsEmoji(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return false;
            }
            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));
        }

        public static string RemoveEmoji(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return "1";
            }
            StringBuilder sc = new StringBuilder();
            int len = source.Length;
            foreach (char item in source)
            {
                if (!IsEmojiCharacter(item))
                {
                    continue; // IsEmojiCharacter 等于false,是Emoji
                }
                sc.Append(item);
            }
            string finalStr= sc.ToString();
            if (string.IsNullOrWhiteSpace(finalStr))
            {
                return "1";
            }
            return finalStr;
        }
    }
}
复制代码

 

使用:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleThreadTimer
{
    class Program
    {
        static void Main(string[] args)
        {
            string testString1 = "条码支付-pos-蜜雪冷饮-    新贝特中国汉堡🍔-000325531004654124070216494150";
            string testString2 = "No emojis here.";

            Console.WriteLine(EmojiUtil.ContainsEmoji  (testString1) ? "Contains Emoji" : "No Emoji");
            Console.WriteLine(EmojiUtil.ContainsEmoji(testString2) ? "Contains Emoji" : "No Emoji");
            Console.ReadKey();
        }

        static bool ContainsEmoji(string s)
        {
            // 这是一个简化的正则表达式,用于匹配多个常见的Emoji范围。  
            // 注意:这可能需要根据最新的Unicode标准进行调整。  
            string emojiPattern = @"[\uD83C\uDF00-\uD83D\uDDFF\u2600-\u27BF]+";
            Regex regex = new Regex(emojiPattern);

            // 使用Regex的IsMatch方法检查字符串中是否包含Emoji  
            return regex.IsMatch(s);
        }
    }
}
复制代码

 

--

posted on   runliuv  阅读(65)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2021-07-03 C#.NET 操作FTP
2021-07-03 使用nginx 中转 https tls1.3 请求
2015-07-03 SQL SERVER 2008安装时出现不能在控件上调用 Invoke 或 BeginInvoke错误 解决方法
2015-07-03 VS2010出现FileTracker : error FTK1011编译错误的解决办法
2013-07-03 ToString yyyy-MM-dd ,MM 小写的故事。
点击右上角即可分享
微信分享提示