java 處理emoji表情信息轉換爲String

2種方式實現:java

注意:若是發現運行時java.lang.NoClassDefFoundError:異常就是缺乏了jar包。添加對應的jar包就能夠。git

1、emoji-java-4.0.0.jar實現github

<!--處理emoji表情-->
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>

<!-- emoji表情依賴類-->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>

代碼:
public static void  main(String args[]){
        String str = "蒼天厚土\uD83D\uDE01 \uD83D\uDC36 \uD83E\uDD14 \uD83D\uDC7B \uD83D\uDE92";
        System.out.println("原始字符爲:\n" + str);

        System.out.println("to aliases 以後:");
        System.out.println(EmojiParser.parseToAliases(str));
        System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.PARSE));
        System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.REMOVE));
        System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.IGNORE));
        System.out.println(EmojiParser.parseToAliases(str, EmojiParser.FitzpatrickAction.IGNORE));
        System.out.println(EmojiParser.parseToUnicode(str));




    }

運行結果:json

原始字符爲:
蒼天厚土😁 🐶 🤔 👻 🚒
to aliases 以後:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土😁 🐶 🤔 👻 🚒測試

 

2、java-emoji-converter-0.1.1.jar方式加密

<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</dependency>

<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>java-emoji-converter</artifactId>
<version>0.1.1</version>
代碼:
public static void  main(String args[]){
        String str = "蒼天厚土\uD83D\uDE01 \uD83D\uDC36 \uD83E\uDD14 \uD83D\uDC7B \uD83D\uDE92";
      

        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        System.out.println("原始字符爲:\n" + str);
        System.out.println("to aliases 以後:");
        System.out.println(emojiConverter.toAlias(str));
        System.out.println(emojiConverter.toUnicode(str));

    }

 

運行結果:spa

原始字符爲:
蒼天厚土😁 🐶 🤔 👻 🚒
to aliases 以後:
蒼天厚土:grin: :dog: :thinking: :ghost: :fire_engine:
蒼天厚土😁 🐶 🤔 👻 🚒code

 

備註:blog

通過測試,神奇的事情發生了,使用方法一加密,方法二能夠正常解密。使用方法二加密,方法一能夠正常解密。應該是2個jar包底層數據一致吧。get

相關文章
相關標籤/搜索