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

当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。强烈推荐。

最近,有不少读者问我是怎么学习的,那我干脆就把我看过的一些优质书籍贡献出来:

  • 计算机基础入门推荐:《程序是怎样跑起来的》、《网络是怎样连接的》、《计算机是怎样跑起来的的》

  • 进一步认识计算机网络:《计算机网络:自顶向下》、《图解http》

  • 数据结构+算法入门:《大话数据结构》、《阿哈算法》

  • 算法进阶:《算法第四版》、《编程珠玑》

由于我是 Java 技术栈的,顺便推荐几本 Java 的书籍,从左到由的顺序看到

  • Java:《Java核心技术卷1》、《编程思想》、《深入理解Java虚拟机》、《effective Java》、《Java并发编程的艺术》

  • 数据库:《mysql必知必会》、《MySQL技术内幕:InnoDB存储引擎》

就先介绍这么多,这些都是最基础最核心的,希望对那些不知道看什么书的同学有所帮助。

对了,我介绍的这些书籍,已经顺便帮你整理好了,你可以在我的原创微信公众号『沉默王二』回复『书籍』获取哦

有收获?希望老铁们来个三连击,给更多的同学看到这篇文章

1、老铁们,关注我的原创微信公众号「沉默王二」,专注于有趣有益的程序人生,保证你看完有所收获,不信你打我。

2、给二哥点个赞呗,可以让更多的人看到这篇文章,顺便激励下我,嘻嘻。

作者info
作者:沉默王二,CSDN 2019年度博客之星,《Web全栈开发进阶之路》作者
原创公众号:『沉默王二』,已写了 250 多篇文章,专注于有趣的 Java 技术和有益的程序人生,期待你的关注。
转载说明:务必注明来源(注明:来源于公众号:沉默王二, 作者:沉默王二)

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉默王二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值