當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.String.offsetByCodePoints()用法及代碼示例


描述

這個java.lang.String.offsetByCodePoints() 方法返回此字符串中從給定索引偏移的索引codePointOffset代碼點。

聲明

以下是聲明java.lang.String.offsetByCodePoints()方法

public int offsetByCodePoints(int index, int codePointOffset)

參數

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

  • codePointOffset- 這是代碼點的偏移量。

返回值

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

異常

IndexOutOfBoundsException- 如果 index 為負或大於此 String 的長度,或者如果 codePointOffset 為正且以 index 開頭的子串少於 codePointOffset 碼點,或如果 codePointOffset 為負且 index 之前的子串少於 codePointOffset 的絕對值代碼點。

示例

下麵的例子展示了 java.lang.String.offsetByCodePoints() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str = "aacdefaa";
      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 = aacdefaa
index = 6

相關用法


注:本文由純淨天空篩選整理自 Java.lang.String.offsetByCodePoints() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。