📜  Java中的 StringBuilder deleteCharAt() 示例

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

Java中的 StringBuilder deleteCharAt() 示例

StringBuilder 类deleteCharAt(int index)方法从 StringBuilder 包含的 String 中删除给定索引处的字符。该方法将 index 作为参数,表示我们要删除的 char 的索引,并将剩余的 String 作为 StringBuilder 对象返回。应用此方法后,此 StringBuilder 会缩短一个字符。

句法:

public StringBuilder deleteCharAt(int index)

参数:此方法接受一个参数index表示要删除的字符的索引。

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

异常:如果索引小于零或索引大于字符串的长度,此方法将抛出StringIndexOutOfBoundsException

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

示例 1:

// Java program to demonstrate
// the deleteCharAt() 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 Character from index 8
        StringBuilder afterRemoval = str.deleteCharAt(8);
  
        // print string after removal of Character
        System.out.println("After removal of character"
                           + " at index 8 = "
                           + afterRemoval.toString());
    }
}
输出:
Before removal String = WelcomeGeeks
After removal of character at index 8 = WelcomeGeks

示例 2:

// Java program to demonstrate
// the deleteCharAt() 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 Character from index 3
        str = str.deleteCharAt(3);
  
        // print string after removal of Character
        System.out.println("After removal of Character"
                           + " from index=3"
                           + " String becomes => "
                           + str.toString());
  
        // remove the substring from index 5
        str = str.deleteCharAt(5);
  
        // print string after removal of Character
        System.out.println("After removal of Character"
                           + " from index=5"
                           + " String becomes => "
                           + str.toString());
    }
}
输出:
Before removal String = GeeksforGeeks
After removal of Character from index=3 String becomes => GeesforGeeks
After removal of Character from index=5 String becomes => GeesfrGeeks

示例 3:演示 IndexOutOfBoundException

// Java program to demonstrate
// exception thrown by the deleteCharAt() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("evil dead_01");
  
        try {
  
            // make index > length of String
            StringBuilder
                afterRemoval
                = str.deleteCharAt(14);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 14

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