当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。