Java - String codePointAt() 方法



描述

Java String codePointAt() 方法用於檢索指定索引處的字元(Unicode 程式碼點)。索引指的是給定字串的 char 值的位置,範圍從 0length()–1

第一個字元值表示 第 0 個索引,第二個字元值表示 第 1 個索引 值,以此類推。codePointAt() 方法接受一個整數引數,該引數儲存索引值。如果索引值為負數或大於字串長度,則會丟擲異常。

語法

以下是 Java String codePointAt() 方法的語法:

public int codePointAt(int index)

引數

  • index − 這是 char 值的索引。

返回值

此方法返回索引處字元的程式碼點值。

檢查特定索引處字元的程式碼點值示例

如果索引值大於 0小於字串長度,則此方法返回指定索引處字元的程式碼點。

在下面的示例中,我們使用值為 "JavaProgramming" 建立一個 string 類 的物件。然後,使用 codePointAt() 方法,我們嘗試檢索索引 3 處的字元程式碼點值。

package com.tutorialspoint.String;

public class Demo {
   public static void main(String[] args) {
      
      //create an object of the string
      String str = new String("JavaProgramming");
      System.out.println("The given string is: " + str);
      
      // initialize the index value
      int index = 3;
      System.out.println("The given index value is: " + index);
      
      //using the codePointAt() method
      System.out.println("The character code point value of the " + index + "rd index is: " + str.codePointAt(index));
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is: JavaProgramming
The given index value is: 3
The character code point value of the 3rd index is: 97

在檢查無效索引處字元的程式碼點值時出現異常示例

如果索引值大於字串長度,則此方法將丟擲 StringIndexOutOfBoundsException

在此示例中,我們使用值為 "HelloWorld" 建立一個字串字面量,然後我們使用 codePointAt() 方法列印指定索引處的字元程式碼點值,該值的索引大於字串長度。

package com.tutorialspoint.String;

public class Test {
   public static void main(String[] args) {
      try {
         
         // string initialization
         String str = "HelloWorld";
         System.out.println("The given string is: " + str);
         
         //initialize the index value
         int index = 15;
         System.out.println("The given index value is: " + index);
         
         //using the codePointAt() method
         System.out.print("The character code point value at the " + index + "th index is: " + str.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given string is: HelloWorld
The given index value is: 15
java.lang.StringIndexOutOfBoundsException: index 15, length 10
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.String.codePointAt(String.java:1545)
	at com.tutorialspoint.String.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: index 15, length 10

使用給定索引處字元的程式碼點值執行 For 迴圈示例

使用 for 迴圈以及 codePointAt() 方法,我們可以列印給定字串的所有字元程式碼點值。

在下面的示例中,我們使用值為 "HelloWorld" 例項化 string 類。然後,使用 codePointAt() 方法以及 for 迴圈來列印給定字串的所有字元程式碼點值。

package com.tutorialspoint.StringBuilder;

public class TP {
   public static void main(String[] args) {
      
      // decalre a string
      String string;
      
      // initialize it
      string = "TutorialsPoint";
      System.out.print("The string value is: " + string);
      
      // declare integer variables
      System.out.print("\nThe uni code point of the characters are: ");
      for (int i = 0; i < string.length(); i++) {
         System.out.print(string.codePointAt(i) + ",");
      }
   }
}

輸出

上述程式產生以下結果:

The string value is: TutorialsPoint
The uni code point of the characters are: 84,117,116,111,114,105,97,108,115,80,111,105,110,116,

在檢查負索引處字元的程式碼點值時出現異常示例

如果給定的索引值為負值,則 codePointAt() 方法將丟擲 StringIndexOutOfBoundsException

package com.tutorialspoint.String;

public class Test {
   public static void main(String[] args) {
      try {
       
         // create an object of the string
         String str = new String("TutorialsPoint");
         System.out.println("The given string is: " + str);
         
         //initialize the index value
         int index = -1;
         System.out.println("The given index value is: " + index);
         
         //using the codePointAt() method
         System.out.print("The character code point value at the " + index + "the index is: " + str.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
          e.printStackTrace();
          System.out.println("Exception: " + e);
      }
   }
}

輸出

執行上述程式後,將產生以下輸出:

The given string is: TutorialsPoint
The given index value is: -1
java.lang.StringIndexOutOfBoundsException: index -1, length 14
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.String.codePointAt(String.java:1545)
	at com.tutorialspoint.String.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: index -1, length 14
java_lang_string.htm
廣告