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-蜜雪冷饮- 新贝特中国汉堡 标签:CORE,codePoint,string,C#,System,using,NET,Emoji From: https://www.cnblogs.com/runliuv/p/18281158