Java StringBuilder delete()方法及示例

来源:这里教程网 时间:2026-02-17 21:14:12 作者:

Java StringBuilder delete()方法及示例

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

语法

public StringBuilder delete(int start, int end)

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

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

返回值 : 该方法在删除子串后返回 这个StringBuilder对象

异常: 如果start小于0,或者start大于String的长度,或者start大于end,该方法会抛出 StringIndexOutOfBoundsException

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

例1 :

// 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());    }}

输出

Before removal String = WelcomeGeeksAfter removal String = Weeeks

例2 :

// 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 = GeeksforGeeksAfter removal of SubString start=8 to end=8 String becomes => GeeksforGeeksAfter removal of SubString start=1 to end=8 String becomes => GGeeks

例3: 演示IndexOutOfBoundException

// 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/lang/StringBuilder.html#delete(int, int)

相关推荐