Java StringBuilder subSequence()方法与示例

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

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

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

  • subSequence() method is used to return the new set of a character sequence that is a subset of this sequence.

    subSequence()方法用于返回字符序列的新集合,该字符序列是该序列的子集。

  • subSequence() 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.

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

  • subSequence() method may throw an exception at the time of character sequence.

    subSequence()方法在字符序列时可能会引发异常。

    IndexOutOfBoundsException – This exception may throw when end_seq > length or when st_seq > end_seq or when st_seq or end_seq are less than 0.

    IndexOutOfBoundsException-当end_seq> length或st_seq> end_seq或st_seq或end_seq小于0时,可能引发此异常。

Syntax:

句法:

    public CharSequence subSequence(int st_seq, int end_seq);

Parameter(s):

参数:

  • int st_seq – represents the beginning index of this character sequence.

    int st_seq –表示此字符序列的开始索引。

  • int end_seq – represents the ending index of this character sequence.

    int end_seq –表示此字符序列的结束索引。

Return value:

返回值:

The return type of this method is CharSequence, it returns the given subsequence of this character sequence.

此方法的返回类型为CharSequence ,它返回此字符序列的给定子序列。

Example:

例:

// Java program to demonstrate the example 
// of subSequence(int st_seq, int end_seq)
// method of StringBuilder 

public class SubSequence {
    public static void main(String[] args) {
        // Creating an StringBuilder object
        StringBuilder st_b = new StringBuilder("Java World ");

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

        // By using subSequence() method is to return the sequence 
        // from the given index 0 to index 3 (but it does not include
        // last index i.e. 4)
        CharSequence cs = st_b.subSequence(0, 4);

        System.out.println("st_b.subSequence(0,4) = " + cs);
    }
}

Output

输出量

st_b = Java World 
st_b.subSequence(0,4) = Java


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值