📜  Java中的 StringBuilder subSequence() 和示例

📅  最后修改于: 2022-05-13 01:54:58.425000             🧑  作者: Mango

Java中的 StringBuilder subSequence() 和示例

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

public CharSequence subSequence?(int start, int end)

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

  1. start是 Integer 类型的值,是指子序列的起始索引。
  2. end是 Integer 类型的值,指的是子序列的最后一个索引。

回报:
此方法返回从 start 到 end-1 范围内的指定子序列。
例外:
如果 start 或 end 为负数,如果 end 大于 length(),或者如果 start 大于 end,则抛出IndexOutOfBoundsException
下面的程序说明了Java.lang.StringBuilder.subSequence() 方法:
示例 1:

Java
// 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 between index 0 to 7
        // using subSequence() and print
        System.out.println("SubSequence = "
                           + str.subSequence(0, 7));
    }
}


Java
// 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 between index 0 to 7
        // using subSequence() and print
        System.out.println("SubSequence = "
                           + str.subSequence(7, 18));
    }
}


Java
// 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 between index 0 to 7
            // using subSequence() and print
            System.out.println(str.subSequence(19, 18));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


输出:

String length = 12 and contains = WelcomeGeeks
SubSequence = Welcome

示例 2:

Java

// 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 between 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

// 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 between 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 Java, int)