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


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


Character.offsetByCodePoints(CharSequence seq,int index,int codePointOffset)是Java中的一種內置方法,該方法返回給定char序列內的索引,該索引與codeIndexOffset代碼點的給定索引偏移。在index和codePointOffset所給定的文本範圍內的未配對替代項均計為一個代碼點。

用法:

public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)

參數:


  • seq–字符序列
  • index–要偏移的索引
  • codePointOffset–代碼點的偏移量

返回值:Character類的此方法返回char序列內的索引。

異常:

  • NullPointerException-如果seq為null。
  • IndexOutOfBoundsException-如果index為負或更大,則字符序列的長度,或者codePointOffset為正,並且以index開頭的子序列少於codePointOffset代碼點,或者codePointOffset為負,並且index之前的子序列小於絕對值codePointOffset代碼點數。

下麵是說明上述方法的程序:
示例1:

// Code to illustrate the offsetByCodePoints() method 
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // Create a CharSequence s and assign value 
        CharSequence s = "Hello World"; 
  
        // Result of offsetByCodePoints on s 
        String str = "The index within the char sequence s is " +  
        Character.offsetByCodePoints(s, 2, 6); 
  
        // Print str value 
        System.out.println(str); 
    } 
}
輸出:
The index within the char sequence s is 8

示例2:

// Code to illustrate the offsetByCodePoints() method 
  
import java.lang.*; 
  
public class gfg { 
  
    public static void main(String[] args) 
    { 
  
        // Create a CharSequence s and assign value 
        CharSequence s = "geeks for geeks"; 
  
        // Result of offsetByCodePoints on s 
        String str = "The index within the char sequence s is " +  
        Character.offsetByCodePoints(s, 3, 8); 
  
        // Print str value 
        System.out.println(str); 
    } 
}
輸出:
The index within the char sequence s is 11


相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 Character.offsetByCodePoints() Method in java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。