您现在的位置是:首页 > 文章详情

推荐,Java Emoji Converter(Emoji表情转换工具)

日期:2018-04-25点击:480
版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/80097901

当Emoji表情字符存储有问题,或者遇到保存字符串到数据库里出现\xF0\x9F\x92\x94类似问题时,请尝试使用这个工具。

什么工具呢?

Java Emoji Converter 。
GitHub地址为:https://github.com/binarywang/java-emoji-converter

可以将Emoji表情(
这里写图片描述

)转义成沉默王二😊这样的。

那,该怎么使用这款工具呢?

首先,在pom.xml文件中引入jar包:

<dependency> <groupId>com.github.binarywang</groupId> <artifactId>java-emoji-converter</artifactId> <version>0.1.1</version> </dependency>

然后,在Java类中声明一个EmojiConverter,如下:

private EmojiConverter emojiConverter = EmojiConverter.getInstance();

使用方法也非常简单,如下:

String str = "  An ����awesome ����string with a few ����emojis!"; String alias = this.emojiConverter.toAlias(str); String html = this.emojiConverter.toHtml(str);

toAlias将Emoji转义为关键字,类似:xiao:
toHtml将Emoji转义为unicode,类似&#128582;

可以直接将toHtml转换后的字符串保存到数据库,显示的时候,就直接显示,不需要再转义,HTML是支持的。整体代码实例如下:

package com.comwer; import com.github.binarywang.java.emoji.EmojiConverter; import junit.framework.Assert; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { private EmojiConverter emojiConverter = EmojiConverter.getInstance(); /** * Create the test case * * @param testName * name of the test case */ public AppTest(String testName) { super(testName); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite(AppTest.class); } /** * Rigourous Test :-) */ public void testApp() { String str = "  An ����awesome ����string with a few ����emojis!"; String alias = this.emojiConverter.toAlias(str); String html = this.emojiConverter.toHtml(str); System.out.println(str); System.out.println(html); System.out.println("EmojiConverterTest.testToAlias()=====>"); System.out.println(alias); Assert.assertEquals( ":no_good: :ok_woman: :couple_with_heart:An :smiley::grinning:awesome :smiley::smiley:string with a few :smiley::wink:emojis!", alias); assertTrue(true); } } 

这种方法的最大好处就是数据库的编码还是utf-8,而不必改成utf8mb4。强烈推荐。


最近正在读明朝那些事儿(第一册——从乞丐和尚到皇帝),恨不得一天把书读完!做皇帝很美,但朱元璋也太能杀人了。乱世的老百姓还是很可怜的。

原文链接:https://yq.aliyun.com/articles/662683
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章