Java - String offsetByCodePoints() 方法



Java String offsetByCodePoints() 方法返回字串物件中從指定索引偏移 codePointOffset 個碼點後的索引。

碼點是 Unicode 系統中用於識別符號號的數值。偏移量是正在使用的儲存區的第一個索引。

文字範圍內的不成對代理項將被視為各一個碼點。

語法

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

public int offsetByCodePoints(int index, int codePointOffset)

引數

  • index − 這是要偏移的索引。

  • codePointOffset − 這是以碼點為單位的偏移量。

返回值

此方法返回此字串中的索引。

獲取按碼點偏移的示例

以下示例演示了 Java String offsetByCodePoints() 方法的用法。這裡我們建立一個值為 'welcome to tutorialspoint' 的字串。然後,我們嘗試列印給定字串中的索引。

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str = "welcome to tutorialspoint";
      System.out.println("string = " + str); 
      
      // returns the index within this String
      int retval = str.offsetByCodePoints(2, 4);      
      
      // prints the index
      System.out.println("index = " + retval);
   }
}

輸出

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

string = welcome to tutorialspoint
index = 6

使用給定索引獲取按碼點偏移的示例

如果我們將索引值作為零傳遞給此方法,它將從開頭返回字串。

在下面的示例中,透過提供索引 0 來計算給定字串序列 'Tutorials Point is a reputed firm!' 的長度:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      
      // Initializing a CharSequence object.
      CharSequence c = "Tutorials Point is a reputed firm!";
      System.out.println("The given string is: " + c);
      
      // Initializing the index and codePointOffset
      int Index = 0;
      int codePointOffset = c.length();
      
      // Getting the index within the given character sequence.
      int res = Character.offsetByCodePoints(c, Index, codePointOffset);
      System.out.println("The resultant length of the string is: " + res);
   }
}

輸出

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

The given string is: Tutorials Point is a reputed firm!
The resultant length of the string is: 34

檢查獲取按碼點偏移時出現的異常示例

如果索引為負數或大於給定序列的長度,則 offsetByCodePoints() 方法將丟擲異常,如下例所示:

public class StringDemo {
   public static void main(String[] args) {
      
      // initializing the string buffer object
      StringBuffer x = new StringBuffer("Coding");
      System.out.println("The string is: " + x);
      
      // getting the offsetByCodePoints on the index -6 and an offset of 8
      int index = x.offsetByCodePoints(-6, 8);
      System.out.println("index of the offset -6, 8 is: " + index);
   }
}

異常

執行上面的程式後,將獲得如下輸出:

The string is: Coding
Exception in thread "main" java.lang.IndexOutOfBoundsException
      at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461)
      at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
      at StringDemo.main(StringDemo.java:7)

檢查碼點偏移量是正數還是負數的示例

以下示例顯示,如果 codePointOffset 為正或負,並且子序列中的索引小於 codePointOffset 碼點,則 offsetByCodePoints() 方法將丟擲異常:

public class StringDemo {
   public static void main(String[] args) {
      
      // initializing the string buffer object
      StringBuffer x = new StringBuffer("Coding");
      System.out.println("The string is: " + x);
      
      // getting the offsetByCodePoints on the index -6 and an offset of 8
      int index = x.offsetByCodePoints(2, 5);
      System.out.println("index of the offset 2, 5 is: " + index);
   }
}

異常

上面程式的輸出如下:

The string is: Coding
Exception in thread "main" java.lang.IndexOutOfBoundsException
      at java.base/java.lang.Character.offsetByCodePoints(Character.java:9342)
      at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:463)
      at java.base/java.lang.StringBuffer.offsetByCodePoints(StringBuffer.java:279)
      at StringDemo.main(StringDemo.java:7)
java_lang_string.htm
廣告