emoji java_emoji-java - Java处理Emoji的类库

emoji-java

68747470733a2f2f7472617669732d63692e6f72672f766475726d6f6e742f656d6f6a692d6a6176612e7376673f6272616e63683d6d617374657268747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f766475726d6f6e742f656d6f6a692d6a6176612e737667687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d5468652532304d49542532304c6963656e73652d627269676874677265656e2e737667

The missing emoji library for java.

emoji-java is a lightweight java library that helps you use Emojis in your java applications.

How to get it?

Via Maven:

com.vdurmont

emoji-java

5.1.1

You can also download the project, build it with mvn clean install and add the generated jar to your buildpath.

Via Gradle:

compile 'com.vdurmont:emoji-java:5.1.1'

Via Direct Download:

Use releases tab to download the jar directly.

How to use it?

EmojiManager

The EmojiManager provides several static methods to search through the emojis database:

getForTag returns all the emojis for a given tag

getForAlias returns the emoji for an alias

getAll returns all the emojis

isEmoji checks if a string is an emoji

containsEmoji 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

Emoji model

An Emoji is a POJO (plain old java object), which provides the following methods:

getUnicode returns the unicode representation of the emoji

getUnicode(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 an UnsupportedOperationException. If the provided Fitzpatrick is null, this method will return the unicode of the emoji.

getDescription returns the (optional) description of the emoji

getAliases returns a list of aliases for this emoji

getTags returns a list of tags for this emoji

getHtmlDecimal returns an html decimal representation of the emoji

getHtmlHexadecimal returns an html decimal representation of the emoji

supportsFitzpatrick returns true if the emoji supports the Fitzpatrick modifiers, else false

Fitzpatrick modifiers

Some emojis now support the use of Fitzpatrick modifiers that gives the choice between 5 shades of skin tones:

Modifier

Type

🏻

type_1_2

🏼

type_3

🏽

type_4

🏾

type_5

🏿

type_6

We defined the format of the aliases including a Fitzpatrick modifier as:

:ALIAS|TYPE:

A few examples:

:boy|type_1_2:

:swimmer|type_4:

:santa|type_6:

EmojiParser

To unicode

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);

System.out.println(result);

// Prints:

// "An 😀awesome 😃string 😄with a few 😉emojis!"

To aliases

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);

System.out.println(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!";

System.out.println(EmojiParser.parseToAliases(str));

System.out.println(EmojiParser.parseToAliases(str, FitzpatrickAction.PARSE));

// Prints twice: "Here is a boy: :boy|type_6:!"

System.out.println(EmojiParser.parseToAliases(str, FitzpatrickAction.REMOVE));

// Prints: "Here is a boy: :boy:!"

System.out.println(EmojiParser.parseToAliases(str, FitzpatrickAction.IGNORE));

// Prints: "Here is a boy: :boy:🏿!"

To html

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);

System.out.println(resultDecimal);

// Prints:

// "An 😀awesome 😃string with a few 😉emojis!"

String resultHexadecimal = EmojiParser.parseToHtmlHexadecimal(str);

System.out.println(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!";

System.out.println(EmojiParser.parseToHtmlDecimal(str));

System.out.println(EmojiParser.parseToHtmlDecimal(str, FitzpatrickAction.PARSE));

System.out.println(EmojiParser.parseToHtmlDecimal(str, FitzpatrickAction.REMOVE));

// Print 3 times: "Here is a boy: 👦!"

System.out.println(EmojiParser.parseToHtmlDecimal(str, FitzpatrickAction.IGNORE));

// Prints: "Here is a boy: 👦🏿!"

The same applies for the methods EmojiParser#parseToHtmlHexadecimal(String) and EmojiParser#parseToHtmlHexadecimal(String, FitzpatrickAction).

Remove emojis

You can easily remove emojis from a string using one of the following methods:

EmojiParser#removeAllEmojis(String): removes all the emojis from the String

EmojiParser#removeAllEmojisExcept(String, Collection): removes all the emojis from the String, except the ones in the Collection

EmojiParser#removeEmojis(String, Collection): removes the emojis in the Collection from the String

For example:

String str = "An 😀awesome 😃string with a few 😉emojis!";

Collection collection = new ArrayList();

collection.add(EmojiManager.getForAlias("wink")); // This is 😉

System.out.println(EmojiParser.removeAllEmojis(str));

System.out.println(EmojiParser.removeAllEmojisExcept(str, collection));

System.out.println(EmojiParser.removeEmojis(str, collection));

// Prints:

// "An awesome string with a few emojis!"

// "An awesome string with a few 😉emojis!"

// "An 😀awesome 😃string with a few emojis!"

Extract Emojis from a string

You can search a string of mixed emoji/non-emoji characters and have all of the emoji characters returned as a Collection.

EmojiParser#extractEmojis(String): returns all emojis as a Collection. This will include duplicates if emojis are present more than once.

Credits

emoji-java originally used the data provided by the github/gemoji project. It is still based on it but has evolved since.

Available Emojis

See a table of the available emojis and their aliases HERE.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值