Java Character offsetByCodePoints()方法
Character.offsetByCodePoints(CharSequence seq, int index, int codePointOffset) 是java中的一个内置方法,用于返回在给定的字符序列中,从给定的索引中偏移的codePointOffset代码点。在index和codePointOffset给定的文本范围内,未配对的代用品各算作一个代码点。
语法
public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
参数
- seq – 字符序列
- index – 要偏移的索引
- codePointOffset – 以代码点为单位的偏移量
返回值: Character类的这个方法返回char序列中的索引。
异常情况 。
- NullPointerException – 如果seq为空。
- IndexOutOfBoundsException – 如果index是负的或者大于char序列的长度,或者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