java Unicodeコードポイントを取得する

java Unicodeコードポイントを取得する

javaで、Unicodeコードポイントを取得する手順を記述してます。「codePointAt​」を使用します。

環境

  • OS windows11 home
  • java 19.0.1

手順

Unicodeコードポイントを取得するには、「codePointAt​」で可能です。

文字列.codePointAt​(位置)

実際に、使用してみます。

public class App {
    public static void main(String[] args) throws Exception {

        String str1 = "mebee";

        System.out.println(str1.codePointAt​(0)); // 109

        for (int i = 0; i < str1.length(); i++) {
            System.out.println(i + ";" + str1.codePointAt​(i));
        }

    }
}

実行結果

サロゲートペア文字

サロゲートペア文字のような通常の2バイトで1文字で表すところを、4バイトで1文字となるものの場合は、位置を2つ指定します。

public class App {
    public static void main(String[] args) throws Exception {

        String str1 = "😂";

        System.out.println(str1.codePointAt​(0)); // 128514
        System.out.println(str1.codePointAt​(1)); // 56834

    }
}