Java StringBuilder offsetByCodePoints() 方法



Java StringBuilder offsetByCodePoints() 方法用於檢索此序列中相對於給定索引偏移 codePointOffset 個程式碼點的索引值

此方法返回序列字元的程式碼點值的流。流包含整數程式碼點值,類似於 Java 中的ASCII 值

offsetByCodePoints() 方法接受兩個作為整數的引數,分別儲存indexcodePointOffset 的值。如果索引值為負或大於序列長度,等等,它會丟擲不同的異常。

語法

以下是Java StringBuilder offsetByCodePoints() 方法的語法:

public int offsetByCodePoints(int index, int codePointOffset)

引數

  • index − 要偏移的索引。

  • codePointOffset − 程式碼點偏移量。

返回值

此方法返回此序列中的索引。

示例:獲取程式碼點偏移量

如果給定的索引值為正小於序列長度,則 offsetByCodePoint() 方法將返回索引值。

在下面的程式中,我們使用值為“TutorialsPoint”建立StringBuilder 類的物件。使用offsetByCodePoints(),我們嘗試獲取指定startIndex 2codePointOffset 5處的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      //create an object of the StringBuilder class
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given String is: " + sb);
      //initialize the index and codePointOffset values
      int index = 2;
      int codePointOffset = 5;
      System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
      //using the offsetByCodePoint() method
      System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
   }
}

輸出

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

The given String is: TutorialsPoint
The given index and code point offset values are: 2 and 5
The index is: 7

示例:獲取程式碼點偏移量時遇到IndexOutOfBoundsException

如果給定的索引值為負大於序列長度,則 offsetByCodePoint() 方法將丟擲 IndexOutOfBoundsException。

在下面的程式中,我們使用值為“Java Programming”例項化StringBuilder 類。使用offsetByCodePoints() 方法,我們嘗試獲取指定index -1codePointOffset 5處的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("Java Programming");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = -1;
         int codePointOffset = 5;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given String is: Java Programming
The given index and code point offset values are: -1 and 5
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException

示例:獲取程式碼點偏移量時遇到IndexOutOfBoundsException

如果給定的索引值大於序列長度,此方法將丟擲IndexOutOfBoundException

在下面的示例中,我們使用值為“Hello”建立一個StringBuilder。使用offsetByCodePoints() 方法,我們嘗試獲取指定startIndex 10codePointOffset 2處的索引值。

public class OffsetCodePoint {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("Hello");
         System.out.println("The given String is: " + sb);
         //initialize the index and codePointOffset values
         int index = 10;
         int codePointOffset = 2;
         System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset);
         //using the offsetByCodePoint() method
         System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception e: " + e);
      }
   }
}

輸出

上述程式產生以下輸出:

The given String is: Hello
The given index and code point offset values are: 10 and 2
java.lang.IndexOutOfBoundsException
   at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
   at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279)
   at OffsetCodePoint.main(OffsetCodePoint.java:15)
Exception e: java.lang.IndexOutOfBoundsException
java_lang_stringbuilder.htm
廣告