Java - String codePointCount() 方法



描述

Java String codePointCount() 方法用於返回此字串指定文字範圍內的 Unicode 程式碼點數(計數)。文字範圍從指定的 beginIndex 開始,擴充套件到索引 endIndex - 1 處的字元。因此,文字範圍的長度(以字元計)為 endIndex-beginIndex

索引指的是字串中的字元位置,第一個字元的值表示第 0 個索引,第二個字元的值表示第 1 個索引,以此類推。codePointCount() 方法接受兩個整數引數,分別儲存 beginIndex 和 endIndex 值。

語法

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

public int codePointCount(int beginIndex, int endIndex)

引數

  • beginIndex − 這是文字範圍第一個字元的索引。

  • endIndex − 這是文字範圍最後一個字元之後的索引。

返回值

此方法返回指定文字範圍內的 Unicode 程式碼點數。

從字串中獲取程式碼點數(索引為零)示例

如果給定的 beginIndex 和 endIndex 值為零,則 codePointcount() 方法返回零。

在下面的程式中,我們建立了一個值為 “HelloWorld” 的字串類物件。然後,使用 codePointCount() 方法,我們嘗試計算給定範圍 (beginIndex = 0, endIndex = 0) 內的字元程式碼點值。

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("HelloWorld");
      System.out.println("The given string is: " + str);
      
      //initialize the beginIndex and endIndex value
      int beginIndex = 0;
      int endIndex = 0;
      System.out.println("The given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
      
      //using codePointCount() method
      System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
   }
}

輸出

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

The given string is: HelloWorld
The given beginIndex and endIndex values are: 0 and 0
The count of character code point at the given range (0,0) is: 0

從字串中獲取程式碼點數時檢查異常示例

如果給定的 beginIndex 值 大於 endIndex 值,則此方法將丟擲 IndexOutOfBoundException 異常。

在這個例子中,我們建立了一個值為 “JavaProgramming” 的字串字面量。然後,使用 codePointCount() 方法,我們嘗試列印給定範圍內的字元程式碼點數,由於 beginIndex 值大於 endIndex 值,因此該方法會丟擲異常。

public class Demo {
   public static void main(String[] args) {
      try {
         
         //string initialization
         String str = "Welcome to Tutorials Point";
         System.out.println("The string value is: " + str);
         
         // initialize the beginIndex and endIndex value
         int beginIndex = 10, endIndex = 5;
         System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
         
         //using the codePointCount() method
         System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
      } catch (Exception e) {
         e.printStackTrace();
         System.out.println("The exception is: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The string value is: Welcome to Tutorials Pointjava.lang.IndexOutOfBoundsException
      at java.base/java.lang.String.codePointCount(String.java:1610)
      at TP.main(TP.java:16)
The exception is: java.lang.IndexOutOfBoundsException

從字串中獲取程式碼點數(非零索引)示例

如果給定的 beginIndex 和 endIndex 值為 正數且小於字串長度,則 codePointCount() 方法將返回給定範圍內的字元程式碼點數。

在下面的示例中,我們用值為 “TutorialsPoint” 例項化字串類。然後,使用 codePointCount() 方法,我們嘗試計算給定範圍 (beginIndex = 2, endIndex = 6) 內的字元程式碼點值。

package com.tutorialspoint.String;
import java.lang.*;
public class Test {
   public static void main (String [] args) {
      
      // instantiate the String class
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      
      //initialize the index values
      int beginIndex = 2, endIndex = 6;
      System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
      System.out.print(
         "The count of the character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: "+ str.codePointCount(beginIndex, endIndex));
   }
}

輸出

上述程式產生以下結果:

The given string is: TutorialsPoint
The beginIndex and endIndex values are: 2 and 6
The count of the character code point at the given range (2,6) is: 4
java_lang_string.htm
廣告