diff --git a/string.go b/string.go index 670d8dd..11d3864 100644 --- a/string.go +++ b/string.go @@ -2120,3 +2120,13 @@ func (ks *LkkString) CountWords(str string) (int, map[string]int) { return total, mp } + +// HasEmoji 字符串是否含有表情符. +func (ks *LkkString) HasEmoji(str string) bool { + return str != "" && RegEmoji.MatchString(str) +} + +// RemoveEmoji 移除字符串中的表情符(使用正则,效率较低). +func (ks *LkkString) RemoveEmoji(str string) string { + return RegEmoji.ReplaceAllString(str, "") +} diff --git a/string_test.go b/string_test.go index 1393c7e..350832f 100644 --- a/string_test.go +++ b/string_test.go @@ -3252,3 +3252,32 @@ func BenchmarkString_EndsWiths(b *testing.B) { KStr.EndsWiths(helloOther2, []string{"", "World 世界!"}, false) } } + +func TestString_HasEmoji_RemoveEmoji(t *testing.T) { + var res string + var chk bool + + chk = KStr.HasEmoji(strHello) + assert.False(t, chk) + + chk = KStr.HasEmoji(tesEmoji1) + assert.True(t, chk) + + res = KStr.RemoveEmoji(tesEmoji1) + chk = KStr.HasEmoji(res) + assert.False(t, chk) +} + +func BenchmarkString_HasEmoji(b *testing.B) { + b.ResetTimer() + for i := 0; i < b.N; i++ { + KStr.HasEmoji(strHello) + } +} + +func BenchmarkString_RemoveEmoji(b *testing.B) { + b.ResetTimer() + for i := 0; i < b.N; i++ { + KStr.RemoveEmoji(tesEmoji1) + } +} \ No newline at end of file diff --git a/testdata.go b/testdata.go index 10bebc2..65a2aff 100644 --- a/testdata.go +++ b/testdata.go @@ -717,6 +717,38 @@ var tesUrl37 = "rtmp://foobar.com" var tesUrl38 = "xyz://foobar.com" var tesUrl39 = "https://www.baidu.com/" +//表情符 +var tesEmoji1 = `Lorem ipsum 🥊dolor 🤒sit amet, consectetur adipiscing 🍂 elit. 🍁🍃🍂🌰🍁🌿🌾🌼🌻سلام تست شد hell中文 +😀😁😂😃😄😅😆😉😊😋😎😍😘😗😙😚☺😇😐😑😶😏😣😥😮😯😪😫😴😌😛😜😝😒😓😔😕😲😷😖😞😟😤😢😭😦😧😨😬😰😱😳😵😡😠 +👦👧👨👩👴👵👶👱👮👲👳👷👸💂🎅👰👼💆💇🙍🙎🙅🙆💁🙋🙇🙌🙏👤👥🚶🏃👯💃👫👬👭💏💑👪 +💪👈👉☝👆👇✌✋👌👍👎✊👊👋👏👐✍ +👣👀👂👃👅👄💋👓👔👕👖👗👘👙👚👛👜👝🎒💼👞👟👠👡👢👑👒🎩🎓💄💅💍🌂 +📱📲📶📳📴☎📞📟📠 +♻🏧🚮🚰♿🚹🚺🚻🚼🚾⚠🚸⛔🚫🚳🚭🚯🚱🚷🔞💈 +🙈🙉🙊🐵🐒🐶🐕🐩🐺🐱😺😸😹😻😼😽🙀😿😾🐈🐯🐅🐆🐴🐎🐮🐂🐃🐄🐷🐖🐗🐽🐏🐑🐐🐪🐫🐘🐭🐁🐀🐹🐰🐇🐻🐨🐼🐾🐔🐓🐣🐤🐥🐦🐧🐸🐊🐢🐍🐲🐉🐳🐋🐬🐟🐠🐡🐙🐚🐌🐛🐜🐝🐞 +💐🌸💮🌹🌺🌻🌼🌷🌱🌲🌳🌴🌵🌾🌿🍀🍁🍂🍃 +🌍🌎🌏🌐🌑🌒🌓🌔🌕🌖🌗🌘🌙🌚🌛🌜☀🌝🌞⭐🌟🌠☁⛅☔⚡❄🔥💧🌊 +🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓🍅🍆🌽🍄🌰🍞🍖🍗🍔🍟🍕🍳🍲🍱🍘🍙🍚🍛🍜🍝🍠🍢🍣🍤🍥🍡🍦🍧🍨🍩🍪🎂🍰🍫🍬🍭🍮🍯🍼☕🍵🍶🍷🍸🍹🍺🍻🍴 +🎪🎭🎨🎰🚣🛀🎫🏆⚽⚾🏀🏈🏉🎾🎱🎳⛳🎣🎽🎿🏂🏄🏇🏊🚴🚵🎯🎮🎲🎷🎸🎺🎻🎬 +😈👿👹👺💀☠👻👽👾💣 +🌋🗻🏠🏡🏢🏣🏤🏥🏦🏨🏩🏪🏫🏬🏭🏯🏰💒🗼🗽⛪⛲🌁🌃🌆🌇🌉🌌🎠🎡🎢🚂🚃🚄🚅🚆🚇🚈🚉🚊🚝🚞🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚚🚛🚜🚲⛽🚨🚥🚦🚧⚓⛵🚤🚢✈💺🚁🚟🚠🚡🚀🎑🗿🛂🛃🛄🛅 +💌💎🔪💈🚪🚽🚿🛁⌛⏳⌚⏰🎈🎉🎊🎎🎏🎐🎀🎁📯📻📱📲☎📞📟📠🔋🔌💻💽💾💿📀🎥📺📷📹📼🔍🔎🔬🔭📡💡🔦🏮📔📕📖📗📘📙📚📓📃📜📄📰📑🔖💰💴💵💶💷💸💳✉📧📨📩📤📥📦📫📪📬📭📮✏✒📝📁📂📅📆📇📈📉📊📋📌📍📎📏📐✂🔒🔓🔏🔐🔑🔨🔫🔧🔩🔗💉💊🚬🔮🚩🎌💦💨 +♠♥♦♣🀄🎴🔇🔈🔉🔊📢📣💤💢💬💭♨🌀🔔🔕✡✝🔯📛🔰🔱⭕✅☑✔✖❌❎➕➖➗➰➿〽✳✴❇‼⁉❓❔❕❗©®™🎦🔅🔆💯🔠🔡🔢🔣🔤🅰🆎🅱🆑🆒🆓ℹ🆔Ⓜ🆕🆖🅾🆗🅿🆘🆙🆚🈁🈂🈷🈶🈯🉐🈹🈚🈲🉑🈸🈴🈳㊗㊙🈺🈵▪▫◻◼◽◾⬛⬜🔶🔷🔸🔹🔺🔻💠🔲🔳⚪⚫🔴🔵 +🐁🐂🐅🐇🐉🐍🐎🐐🐒🐓🐕🐖 +♈♉♊♋♌♍♎♏♐♑♒♓⛎ +🕛🕧🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦⌛⏳⌚⏰⏱⏲🕰 +💘❤💓💔💕💖💗💙💚💛💜💝💞💟❣ +💐🌸💮🌹🌺🌻🌼🌷🌱🌿🍀 +🌿🍀🍁🍂🍃 +🌑🌒🌓🌔🌕🌖🌗🌘🌙🌚🌛🌜🌝 +🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓 +💴💵💶💷💰💸💳 +🚂🚃🚄🚅🚆🚇🚈🚉🚊🚝🚞🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚚🚛🚜🚲⛽🚨🚥🚦🚧⚓⛵🚣🚤🚢✈💺🚁🚟🚠🚡🚀 +🏠🏡🏢🏣🏤🏥🏦🏨🏩🏪🏫🏬🏭🏯🏰💒🗼🗽⛪🌆🌇🌉 +📱📲☎📞📟📠🔋🔌💻💽💾💿📀🎥📺📷📹📼🔍🔎🔬🔭📡📔📕📖📗📘📙📚📓📃📜📄📰📑🔖💳✉📧📨📩📤📥📦📫📪📬📭📮✏✒📝📁📂📅📆📇📈📉📊📋📌📍📎📏📐✂🔒🔓🔏🔐🔑 +⬆↗➡↘⬇↙⬅↖↕↔↩↪⤴⤵🔃🔄🔙🔚🔛🔜🔝` +var tesEmoji2 = `Hi!😀👽😀☂❤华み원❤This is a string 😄 🐷 with some 👍🏻 🙈 emoji! 🐷 🏃🏿‍♂️` + func init() { gofakeit.Struct(&personS1) gofakeit.Struct(&personS2)