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


Java StringBuilder subSequence()用法及代码示例


StringBuilder类的subSequence(int start,int end)方法是一种内置方法,用于返回位于此序列的索引start和end-1之间的字符的子序列。子序列以索引开头的char值开始,以(end-1)的char值结束。返回的子序列的长度为end-start。因此,如果开始等于结束,则返回一个空的子序列。

用法:

public CharSequence subSequence?(int start, int end)

参数:
此方法接受两个参数:


  1. startInteger类型值是指子序列的起始endex。
  2. end整数类型值是指子序列的最后一个endex。

返回值:
此方法返回范围从start到end-1的指定子序列。
异常:
如果start或end为负,或者end大于length(),或者start大于end,则抛出IndexOutOfBoundsException。

以下程序说明了java.lang.StringBuilder.subSequence()方法:
示例1:

// Java program to demonstrate 
// the subSequence() Method. 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("WelcomeGeeks"); 
  
        // print string 
        System.out.println("String contains = " + str); 
  
        // get subSequence bewteen index 0 to 7 
        // using subSequence() and print 
        System.out.println("SubSequence = "
                           + str.subSequence(0, 7)); 
    } 
}

输出:

String length = 12 and contains = WelcomeGeeks
SubSequence = Welcome

示例2:

// Java program to demonstrate 
// the subSequence() Method. 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("Indian Team Played Well"); 
  
        // print string 
        System.out.println("String contains = " + str); 
  
        // get subSequence bewteen index 0 to 7 
        // using subSequence() and print 
        System.out.println("SubSequence = "
                           + str.subSequence(7, 18)); 
    } 
}

输出:

String contains = Indian Team Played Well
SubSequence = Team Played

示例3:当开始>结束时:

// Java program to demonstrate 
// Exception thrown by the subSequence() Method. 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("Indian Team Played Well"); 
  
        try { 
            // get subSequence bewteen index 0 to 7 
            // using subSequence() and print 
            System.out.println(str.subSequence(19, 18)); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
}

输出:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:935)
    at java.lang.StringBuilder.substring(StringBuilder.java:76)
    at java.lang.AbstractStringBuilder.subSequence(AbstractStringBuilder.java:912)
    at java.lang.StringBuilder.subSequence(StringBuilder.java:76)
    at GFG.main(File.java:16)

参考文献:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#subSequence(int,int)



相关用法


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