如何使用Java確定字串中的Unicode程式碼點



問題描述

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

解決方案

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

public class StringUniCode {
   public static void main(String[] args) {
      String test_string = "Welcome to TutorialsPoint";
      System.out.println("String under test is = "+test_string);
      
      System.out.println("Unicode code point at" 
         +" position 5 in the string is = "
         +  test_string.codePointBefore(5));
   }
}

結果

以上程式碼示例將產生以下結果。

String under test is = Welcome to TutorialsPoint
Unicode code point at position 5 in the string is =111
java_strings.htm
廣告
© . All rights reserved.