Ported emoji library from iris:emoji-java
Via NuGet:
- Manage NuGet Packages: Search and install EmojiCSharp
- Package Manager: Install-Package EmojiCSharp -Version 1.0.1
- .NET CLI: dotnet add package EmojiCSharp --version 1.0.1
- PackageReference:
<PackageReference Include="EmojiCSharp" Version="1.0.1" />
The EmojiManager
provides several static methods to search through the emojis database:
Init
initialize a database, run it once inMain
GetForTag
returns all the emojis for a given tagGetForAlias
returns the emoji for an aliasGetAll
returns all the emojisIsEmoji
checks if a string is an emojiContainsEmoji
checks if a string contains any emoji
You can also query the metadata:
GetAllTags
returns the available tags
Or get everything:
GetAll
returns all the emojis
Unicode
the unicode respresentation of the emojiGetUnicode(Fitzpatrick)
returns the unicode representation of the emoji with the provided Fitzpatrick modifier. If the emoji doesn't support the Fitzpatrick modifiers, this method will throw anInvalidOperationException
. If Fitzpatrick is invalid, this method will throw anArgumentException
Description
the (optional) description of the emojiAliases
a list of aliases for this emojiTags
a list of tags for this emojiHtmlDecimal
an html decimal representation of the emojiHtmlHexadecimal
an html decimal representation of the emojiSupportsFitzpatrick
true if the emoji supports the Fitzpatrick modifiers, else false
Some emojis now support the use of Fitzpatrick modifiers that gives the choice between 5 shades of skin tones:
Modifier | Types |
---|---|
🏻 | type12, type_1_2 |
🏼 | type3, type_3 |
🏽 | type4, type_4 |
🏾 | type5, type_5 |
🏿 | type6, type_6 |
We defined the format of the aliases including a Fitzpatrick modifier as:
:ALIAS|TYPE:
A few examples:
:boy|type_1_2:
:fist|type12:
:swimmer|type4:
:santa|type_6:
To replace all the aliases and the html representations found in a string by their unicode, use EmojiParser#ParseToUnicode(String)
.
For example:
string str = "An :grinning:awesome :smiley:string 😄with a few :wink:emojis!";
string result = EmojiParser.ParseToUnicode(str);
Console.WriteLine(result);
// Prints:
// "An 😀awesome 😃string 😄with a few 😉emojis!"
To replace all the emoji's unicodes found in a string by their aliases, use EmojiParser#ParseToAliases(String)
.
For example:
string str = "An 😀awesome 😃string with a few 😉emojis!";
string result = EmojiParser.ParseToAliases(str);
Console.WriteLine(result);
// Prints:
// "An :grinning:awesome :smiley:string with a few :wink:emojis!"
By default, the aliases will parse and include any Fitzpatrick modifier that would be provided. If you want to remove or ignore the Fitzpatrick modifiers, use EmojiParser#ParseToAliases(String, FitzpatrickAction)
. Examples:
string str = "Here is a boy: \uD83D\uDC66\uD83C\uDFFF!";
Console.WriteLine(EmojiParser.ParseToAliases(str));
Console.WriteLine(EmojiParser.ParseToAliases(str, FitzpatrickAction.Parse));
// Prints twice: "Here is a boy: :boy|type_6:!"
Console.WriteLine(EmojiParser.ParseToAliases(str, FitzpatrickAction.Remove));
// Prints: "Here is a boy: :boy:!"
Console.WriteLine(EmojiParser.ParseToAliases(str, FitzpatrickAction.Ignore));
// Prints: "Here is a boy: :boy:🏿!"
To replace all the emoji's unicodes found in a string by their html representation, use EmojiParser#ParseToHtmlDecimal(String)
or EmojiParser#ParseToHtmlHexadecimal(String)
.
For example:
string str = "An 😀awesome 😃string with a few 😉emojis!";
string resultDecimal = EmojiParser.ParseToHtmlDecimal(str);
Console.WriteLine(resultDecimal);
// Prints:
// "An 😀awesome 😃string with a few 😉emojis!"
string resultHexadecimal = EmojiParser.ParseToHtmlHexadecemal(str);
Console.WriteLine(resultHexadecimal);
// Prints:
// "An 😀awesome 😃string with a few 😉emojis!"
By default, any Fitzpatrick modifier will be removed. If you want to ignore the Fitzpatrick modifiers, use EmojiParser#ParseToAliases(String, FitzpatrickAction)
. Examples:
string str = "Here is a boy: \uD83D\uDC66\uD83C\uDFFF!";
Console.WriteLine(EmojiParser.ParseToHtmlDecimal(str));
Console.WriteLine(EmojiParser.ParseToHtmlDecimal(str, FitzpatrickAction.Parse));
Console.WriteLine(EmojiParser.ParseToHtmlDecimal(str, FitzpatrickAction.Remove));
// Print 3 times: "Here is a boy: 👦!"
Console.WriteLine(EmojiParser.ParseToHtmlDecimal(str, FitzpatrickAction.Ignore));
// Prints: "Here is a boy: 👦🏿!"
The same applies for the methods EmojiParser#ParseToHtmlHexadecemal(String)
and EmojiParser#ParseToHtmlHexadecemal(String, FitzpatrickAction)
.
You can easily remove emojis from a string using one of the following methods:
EmojiParser#RemoveAllEmojis(String)
: removes all the emojis from the stringEmojiParser#RemoveAllEmojisExcept(String, IEnumerable<Emoji>)
: removes all the emojis from the string, except the ones in the listEmojiParser#RemoveEmojis(String, IEnumerable<Emoji>)
: removes the emojis in the list from the string
For example:
string str = "An 😀awesome 😃string with a few 😉emojis!";
List<Emoji> list = new List<Emoji>();
list.Add(EmojiManager.GetForAlias("wink")); // This is 😉
Console.WriteLine(EmojiParser.RemoveAllEmojis(str));
Console.WriteLine(EmojiParser.RemoveAllEmojisExcept(str, list));
Console.WriteLine(EmojiParser.RemoveEmojis(str, list));
// Prints:
// "An awesome string with a few emojis!"
// "An awesome string with a few 😉emojis!"
// "An 😀awesome 😃string with a few emojis!"
You can search a string of mixed emoji/non-emoji characters and have all of the emoji characters returned as a list.
EmojiParser#ExtractEmojis(String)
: returns all emojis as a list. This will include duplicates if emojis are present more than once.
emoji-csharp is based on iris-emoji-java
iris-emoji-java is based on github/vdurmont/emoji-java.
And in its turn emoji-java originally used the data provided by the github/gemoji project. It is still based on it but has evolved since.