How to count EMOJI in a string or text line and display the output, how many emoji are in the text line or string?
For example: "Hello 😀😁😂🤣😃😄😅 there"
The output should be: 7
Emojis or Unicode. Standard emojis count as two characters when writing a message.
Swift 5 Scalars have isEmoji and isEmojiPresentation properties that will help to find emoji in particular String. isEmoji - Boolean value indicating whether the scalar has an emoji presentation, whether or not it is the default.
Emojis are a modern text in that they have not been a longstanding, traditional form of communication.
During text entry, type Windows logo key + . (period). The emoji keyboard will appear. Select an emoji with the mouse, or keep typing to search through the available emojis for one you like.
One option would be to compare the length of the input string against the length of the same string with all Emoji characters removed:
function fancyCount(str){
return Array.from(str.split(/[\ufe00-\ufe0f]/).join("")).length;
}
var input = "Hello 🍌🍌🍌🍌🍌🍌🍌 there";
var output = input.replace(/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g, "");
console.log("number of Emoji: " + (fancyCount(input) - fancyCount(output)));
I give massive credit to this helpful blog post, which provided the fancyCount()
function. This function can detect that certain Emoji characters actually have a width of 2, while other characters have a width of 1. The issue here is one of encoding. Some Emoji characters may take up two bytes, whereas a basic ASCII character (e.g. A-Z) would only take up one byte.
You can try this lib https://github.com/mathiasbynens/emoji-regex It provides emojiRegex. So you can match emojis in your test like this:
const text = "Hello 😀😁😂🤣😃😄😅 there"
const regex = emojiRegex();
regex.exec(text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With