Java StringBuilder offsetByCodePoints()方法与示例
StringBuilder类offsetByCodePoints()方法
offsetByCodePoints()方法在java.lang包中可用。
offsetByCodePoints()方法用于检索此对象包含的字符序列集中的索引,该索引与代码点偏移量代码点作为参数传递的索引之间存在偏移。
offsetByCodePoints()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
在按代码点返回索引时,offsetByCodePoints()方法可能会引发异常。IndexOutOfBoundsException-此异常可能引发
情况1:当第一个参数索引<0时,大于length()或大于>结束。
情况2:当第二个参数cp_off>0时,索引之前的子序列少于cp_off代码点。
情况3:当第二个参数cp_off<0时,索引前的子序列具有小于cp_off代码点的绝对值。
语法:
public int offsetByCodePoints(int indices, int cp_off);
参数:
intindex–表示要偏移的索引
intcp_off–表示代码点偏移量
返回值:
此方法的返回类型为int,它返回此序列内的索引。
示例
//Java程序演示示例
//intoffsetByCodePoints(intindex,intcp_off)的数量
//StringBuilder的方法
public class OffsetByCodePoints {
public static void main(String[] args) {
int indices = 3;
int cp_off = 7;
//创建一个StringBuilder对象
StringBuilder st_b = new StringBuilder("Java World ");
//显示st_b-
System.out.println("st_b = " + st_b);
//通过使用offsetByCodePoints(indices,cp_off)方法可以
//此范围内的索引
int res = st_b.offsetByCodePoints(indices, cp_off);
//显示st_b-
System.out.println("st_b.offsetByCodePoints(indices,cp_off) = " + res);
}
}输出结果
st_b = Java World st_b.offsetByCodePoints(indices,cp_off) = 10
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短