📜  Java中的 StringBuilder delete() 示例

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

Java中的 StringBuilder delete() 示例

StringBuilder 类的delete(int start, int end)方法从StringBuilder 包含的String 中删除从index start 到index end-1 的字符。该方法以两个索引为参数,first start 表示第一个字符的索引,endIndex 表示要从 StringBuilder 包含的 String 中删除的子字符串的最后一个字符之后的索引,并将剩余的 String 作为 StringBuilder 对象返回。

句法:

public StringBuilder delete(int start, int end)

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

  • start :子字符串第一个字符的索引。
  • end :子字符串最后一个字符之后的索引。

返回值:此方法在删除子字符串后返回此 StringBuilder 对象

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

下面的程序演示了 StringBuilder 类的 delete() 方法:

示例 1:

Java
// Java program to demonstrate
// the delete() 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("Before removal String = "
                           + str.toString());
 
        // remove the substring from index 2 to 8
        StringBuilder afterRemoval = str.delete(2, 8);
 
        // print string after removal of substring
        System.out.println("After removal String = "
                           + afterRemoval.toString());
    }
}


Java
// Java program to demonstrate
// the delete() 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("Before removal String = "
                           + str.toString());
 
        // remove the substring from index 8 to 8
        StringBuilder afterRemoval = str.delete(8, 8);
 
        // start equal to end so no change in string
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=8 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
 
        // remove the substring from index 1 to 8
        afterRemoval = str.delete(1, 8);
 
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=1 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
    }
}


Java
// Java program to demonstrate
// exception thrown by the delete() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("GeeksforGeeks");
 
        try {
 
            // make start greater than end
            StringBuilder afterRemoval = str.delete(7, 4);
        }
 
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


输出
Before removal String = WelcomeGeeks
After removal String = Weeeks

示例 2:

Java

// Java program to demonstrate
// the delete() 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("Before removal String = "
                           + str.toString());
 
        // remove the substring from index 8 to 8
        StringBuilder afterRemoval = str.delete(8, 8);
 
        // start equal to end so no change in string
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=8 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
 
        // remove the substring from index 1 to 8
        afterRemoval = str.delete(1, 8);
 
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=1 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
    }
}
输出:
Before removal String = GeeksforGeeks
After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks
After removal of SubString start=1 to end=8 String becomes => GGeeks

示例 3:演示 IndexOutOfBoundException

Java

// Java program to demonstrate
// exception thrown by the delete() Method.
 
class GFG {
    public static void main(String[] args)
    {
 
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("GeeksforGeeks");
 
        try {
 
            // make start greater than end
            StringBuilder afterRemoval = str.delete(7, 4);
        }
 
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException

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