Java StringBuffer codePointAt() 方法



Java 的StringBuffer codePointAt() 方法用於獲取 StringBuffer 中指定索引處的字元(其 Unicode 程式碼點)。索引範圍從 0 到 length() - 1。

注意 - 如果在給定索引處指定的字元值位於高代理範圍,並且其後續索引小於此序列的長度,並且此後續索引處的字元值位於低代理範圍,則返回對應於此代理對的補充程式碼點。否則,返回給定索引處的字元值。

語法

以下是 Java StringBuffer codePointAt() 方法的語法

public int codePointAt(int index)

引數

  • index - 這是字元值的索引。

返回值

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

示例:獲取給定索引處的程式碼點

如果我們使用字母作為 CharSequence 初始化 StringBuffer 物件,則該方法返回 StringBuffer 引數索引處存在的字元。

以下示例顯示了 Java StringBuffer codePointAt() 方法的使用。

package com.tutorialspoint;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer buff = new StringBuffer("TUTORIALS");
      System.out.println("buffer = " + buff);

      // returns the codepoint at index 5
      int retval = buff.codePointAt(5);
      System.out.println("Character(unicode point) = " + retval);
    
      buff = new StringBuffer("amrood admin ");
      System.out.println("buffer = " + buff);
  
      // returns the codepoint at index 6 i.e whitespace character
      retval = buff.codePointAt(6);
      System.out.println("Character(unicode point) = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

buffer = TUTORIALS
Character(unicode point) = 73
buffer = amrood admin
Character(unicode point) = 32

示例:獲取給定索引處的程式碼點

在另一個示例中,我們建立一個 StringBuffer 物件並使用數字序列對其進行初始化。然後,使用索引作為其引數在此物件上呼叫該方法。

public class StringBufferCodePointAt {

   public static void main(String[] args) {

      StringBuffer obj = new StringBuffer("78343890");

      int id = 5;

      System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id));
   }
}

輸出

一旦我們編譯並執行上面的程式,輸出將顯示如下:

The code point at index 5 is: 56

示例:獲取給定索引處的程式碼點

如果我們使用符號作為 CharSequence 初始化 StringBuffer 物件,並將索引作為引數傳遞,則該方法將返回該索引處存在的字元。

public class StringBufferCodePointAt {

   public static void main(String[] args) {

      StringBuffer obj = new StringBuffer("#$%^&*(");

      int id = 3;

      System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id));
   }
}

輸出

上述程式的輸出列印為:

The code point at index 3 is: 94

示例:在獲取負索引處的程式碼點時遇到異常

如果作為引數傳遞給此方法的索引無效(為負或大於字元序列長度),則會丟擲異常。

public class StringBufferCodePointAt {

   public static void main(String[] args) {

      StringBuffer obj = new StringBuffer("Tutorialspoint");

      int id = -2;

      System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id)); //throws exception
   }
}

異常

如果我們嘗試編譯並執行上述程式,則會丟擲異常而不是列印輸出:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index -2 out of bounds for length 14
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.lang.String.checkIndex(String.java:4570)
        at java.base/java.lang.AbstractStringBuilder.codePointAt(AbstractStringBuilder.java:389)
        at java.base/java.lang.StringBuffer.codePointAt(StringBuffer.java:252)
        at StringBufferCodePointAt.main(StringBufferCodePointAt.java:11)
java_lang_stringbuffer.htm
廣告