字串unicode

2019-10-16 22:31:27

在Java中,如何確定字串中的Unicode程式碼點?

此範例顯示如何使用codePointBefore()方法在指定的索引之前返回字元(Unicode程式碼點)。

package com.yiibai;

public class StringUniCode {
    public static void main(String[] args) {
        String test_string = "Welcome to Tw511.com";
        System.out.println("String under test is = " + test_string);

        System.out.println("Unicode code point at"
                + " position 15 in the string is = "
                + test_string.codePointBefore(15));
    }
}

執行上面範例程式碼,得到以下結果 -

String under test is = Welcome to Tw511.com
Unicode code point at position 5 in the string is = 98