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

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

什么工具呢?

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

可以将Emoji表情(

)转义成**沉默王二&amp#128522;**这样的。

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

首先,在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,类似?。

可以直接将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。强烈推荐。
lib包地址:
https://www.baidu.com/link?url=kxuz4GCJKAVkMCT1DfNMotOnrLrVl-8f3qx109Z97QVsZT7rugsOM0VDtas0sESKyihKwO9JoDsdPCHLMzaHmZEq9VpXstCT15aZgpFKEpeWGUye2hBgna_VFccw6tJH&wd=&eqid=cb308fe50004ab23000000065da41f5b

https://blog.csdn.net/qing_gee/article/details/80097901

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值