📜  Java中的 StringBuilder substring() 方法及示例

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

Java中的 StringBuilder substring() 方法及示例

在 StringBuilder 类中,根据传递给它的参数,有两种类型的子字符串方法。

子字符串(整数开始)

StringBuilder 类substring(int start)方法是用于返回从索引 start 开始并延伸到该序列末尾的子字符串的内置方法。此方法返回的字符串包含从索引开始到旧序列结束的所有字符。

句法:

public String substring(int start)

参数:该方法只接受一个参数start ,它是Integer类型的值是指子串的起始索引。

返回值:此方法返回位于旧序列开始到结束范围内的子字符串

异常:如果 start 小于零或大于此对象的长度,则此方法抛出StringIndexOutOfBoundsException

下面的程序说明了 StringBuilder substring() 方法:

示例 1:

// Java program to demonstrate
// the substring() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("GeeksForGeeks");
  
        // print string
        System.out.println("String contains = "
                           + str);
  
        // get substring start from index 5
        // using substring() and print results
        System.out.println("SubSequence = "
                           + str.substring(5));
    }
}
输出:
String contains = GeeksForGeeks
SubSequence = ForGeeks

示例 2:演示 StringIndexOutOfBoundsException

// Java program to demonstrate
// Exception thrown by the substring() 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 substring starts from index -7
            // using substring() and print
            str.substring(-7);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -7

子字符串(int 开始,int 结束)

StringBuilder 类substring(int start, int end)方法是用于返回从索引 start 开始并扩展到该序列的索引 end-1 的子字符串的内置方法。此方法返回的字符串包含旧序列的索引 start 到索引 end-1 的所有字符。

句法:

public String substring(int start)

参数:该方法接受两个参数start是 Integer 类型的 value 是指子字符串的开始索引, end也是 Integer 类型的 value 是指子字符串的结束索引。

返回值:此方法返回位于旧序列索引开始到索引结束1的范围内的子字符串

异常:如果 start 或 end 为负数或大于 length(),或者 start 大于 end,则此方法抛出StringIndexOutOfBoundsException

下面的程序说明了 StringBuilder.substring() 方法:

示例 1:

// Java program to demonstrate
// the substring() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder str
            = new StringBuilder("GeeksForGeeks");
  
        // print string
        System.out.println("String contains = " + str);
  
        // get substring start from index 5 to index 8
        // using substring() and print results
        System.out.println("SubSequence = "
                           + str.substring(5, 8));
    }
}
输出:
String contains = GeeksForGeeks
SubSequence = For

示例 2:演示 StringIndexOutOfBoundsException

// Java program to demonstrate
// Exception thrown by the substring() 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 substring starts from index 7
            // and end at index 3
            // using substring() and print
            str.substring(9, 3);
        }
  
        catch (Exception e) {
  
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -6

参考:
https://docs.oracle.com/javase/10/docs/api/java Java, int)
https://docs.oracle.com/javase/10/docs/api/java Java)