當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringBuilder deleteCharAt()用法及代碼示例


StringBuilder類的deleteCharAt(int index)方法從StringBuilder包含的String中刪除給定索引處的字符。此方法將index作為參數,該參數表示我們要刪除的char的索引,並將剩餘的String作為StringBuilder對象返回。應用此方法後,此StringBuilder縮短一個字符。

用法:

public StringBuilder deleteCharAt(int index)

參數:此方法接受一個參數索引,該參數代表要刪除的字符的索引。


返回值:刪除字符後,此方法返回此StringBuilder對象。

異常:如果索引小於零或索引大於String的長度,則此方法將引發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/lang/StringBuilder.html#deleteCharAt(int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder deleteCharAt() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。