Java StringBuilder offsetByCodePoints()方法与示例

StringBuilder类offsetByCodePoints()方法 (StringBuilder Class offsetByCodePoints() method)

  • offsetByCodePoints() method is available in java.lang package.

    offsetByCodePoints()方法在java.lang包中可用。

  • offsetByCodePoints() method is used to retrieve the index within the set of character sequence contained by this object that is offset from the indices passed as an argument by code point offset code points.

    offsetByCodePoints()方法用于检索此对象包含的字符序列集中的索引,该索引与代码点偏移量代码点作为参数传递的索引偏移。

  • offsetByCodePoints() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    offsetByCodePoints()方法是一个非静态方法,仅可通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • offsetByCodePoints() method may throw an exception at the time of returning an index by code points. IndexOutOfBoundsException – This exception may throw

    在按代码点返回索引时, offsetByCodePoints()方法可能会引发异常。 IndexOutOfBoundsException-此异常可能引发

    • Case 1: when the first parameter indices < 0, greater than length() or beg > end.
    • 情况1:当第一个参数索引<0时 ,大于length()或beg> end 。
    • Case 2: when the second parameter cp_off > 0, subsequence before indices has fewer than cp_off code points.
    • 情况2:当第二个参数cp_off> 0时 ,索引之前的子序列少于cp_off代码点。
    • Case 3: when the second parameter cp_off < 0, subsequence before indices has fewer than cp_off code points absolute value.
    • 情况3:当第二个参数cp_off <0时 ,索引前的子序列具有小于cp_off代码点的绝对值。

Syntax:

句法:

    public int offsetByCodePoints(int indices, int cp_off);

Parameter(s):

参数:

  • int indices – represents the index to be offset

    int index –表示要偏移的索引

  • int cp_off – represents the code point offset

    int cp_off –表示代码点偏移量

Return value:

返回值:

The return type of this method is int, it returns the indices within this sequence.

此方法的返回类型为int ,它返回此序列内的索引。

Example:

例:

// Java program to demonstrate the example 
// of int offsetByCodePoints(int indices, int cp_off)
// method of StringBuilder 

public class OffsetByCodePoints {
    public static void main(String[] args) {
        int indices = 3;
        int cp_off = 7;

        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World ");

        // Display st_b 
        System.out.println("st_b = " + st_b);

        // By using offsetByCodePoints(indices,cp_off) method is to
        // return the index within this range in st_b
        int res = st_b.offsetByCodePoints(indices, cp_off);

        // Display st_b
        System.out.println("st_b.offsetByCodePoints(indices,cp_off) = " + res);
    }
}

Output

输出量

st_b = Java World 
st_b.offsetByCodePoints(indices,cp_off) = 10


翻译自: https://www.includehelp.com/java/stringbuilder-offsetbycodepoints-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值