LoginSignup
5
3

More than 5 years have passed since last update.

Javaで文字列に絵文字が含まれているか判定する

Last updated at Posted at 2018-05-19

いかにして絵文字を判定するか

絵文字,環境によって表示できなかったりして嫌ですよね💩
一旦文字コードをEUC_JPに変換して元に戻し,元の文字列と比較することで判定が可能です👍🏻

実装

isIncludeEmoji.java
public static boolean isIncludeEmoji(String str){
    try{
        byte[] bytes = str.getBytes("EUC_JP"); //EUC_JPでバイト配列に変換
        String newStr = new String(bytes, "EUC_JP"); //文字列に再変換
        return(!str.equals(newStr)); 
    }catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }
    return true;
}
サンプル.java
System.out.println(isIncludeEmoji("ひよこちゃん🐣")); //true
System.out.println(isIncludeEmoji("虚無")); //false

間違っていたら教えて頂けると幸いです🙏🏼

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3