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


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


Character.offsetByCodePoints(char [] a,int start,int count,int index,int codePointOffset)是Java中的一种内置方法,它返回给定char子数组内的索引,该子数组与codeIndexOffset代码点相对于给定索引的偏移量。 start和count参数指定char数组的子数组。在index和codePointOffset所给定的文本范围内的未配对替代项均计为一个代码点。
字符类的offsetByCodePoints(char [] a,int起始,int计数,int索引,int codePointOffset)方法是静态的,因此应进行静态访问。
通常通过声明method_name(argument)来调用非静态方法。但是在这种情况下,由于它是一个静态方法,因此在调用期间将类名作为后缀附加。如果尝试以非静态方式调用java offsetByCodePoints()方法,可能会遇到编译问题。

用法:

public static int offsetByCodePoints(char[] a, int start, int count, int index,
int codePointOffset)

参数:


  • a:字符数组
  • start:子数组的第一个字符的索引
  • count:子数组的长度(以字符为单位)
  • index:要偏移的索引
  • codePointOffset:代码点的偏移量

返回值:此方法在子数组内返回整数类型值,即索引。

异常:

  • NullPointerException:如果a为null。
  • IndexOutOfBoundsException:如果start或count为负,或者start + count大于给定数组的长度,或者index小于start或更大,则start + count,或者codePointOffset为正且文本范围以index开头且以start + count结尾– 1的数量少于codePointOffset代码点,或者如果codePointOffset为负,并且以index – 1开头和结尾的文本范围小于codePointOffset代码点的绝对值。

以下示例程序旨在说明Character.offsetByCodePoints()方法:

//Java program for offsetByCodePoints() method 
import java.lang.*; 
public class gfg { 
   public static void main(String[] args) { 
      // Creating a char array c_arr and assigning values 
      char[] c_arr = new char[] {'g','e','e','k','s'}; 
  
      // Integer primitives  
      int s = 1; 
      int c= 4; 
  
      // Creating and assigning result of offsetByCodePoints 
      // On subarray of c_arr to r 
     int r = Character.offsetByCodePoints(c_arr, s, c, 1, 2); 
  
      String st = "The index within the subarray is " + r; 
  
      System.out.println(st); 
   } 
}
输出:
The index within the subarray is 3

参考:https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#offsetByCodePoints(char[],%20int,%20int,%20int,%20int)



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Character.offsetByCodePoints() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。