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


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


StringBuilder類的replace(int start,int end,String str)方法用於用指定String中的字符替換此序列的子字符串中的字符。子字符串從指定的索引開頭開始,並擴展到索引結尾– 1處的字符,或者擴展到序列末尾(如果不存在這樣的字符)。首先,刪除子字符串的字符,並插入作為參數傳遞的字符串來代替這些字符。
用法:

public StringBuilder replace?(int start, int end, String str)

參數:
此方法接受三個參數:

  1. start–引用起始索引的整數類型值。
  2. end–引用結束索引的整數類型值。
  3. str–字符串類型值,該值引用將替換先前內容的字符串。

返回值:
成功替換字符後,此方法返回StringBuilder對象。
異常:
如果開始為負,大於length(),或者大於結束,則StringIndexOutOfBoundsException。


以下程序說明了java.lang.StringBuilder.replace()方法:
示例1:

// Java program to demonstrate 
// the replace() 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("String = "
                           + str.toString()); 
  
        // replace Character from index 1 to 7 by "e are " 
        StringBuilder strReturn = str.replace(1, 7, "e are "); 
  
        // print string 
        System.out.println("After Replace() String = "
                           + strReturn.toString()); 
    } 
}

輸出:

String = WelcomeGeeks
After Replace() String = We are Geeks

示例2:

// Java program to demonstrate 
// the replace() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("Tony Stark will die"); 
  
        // print string 
        System.out.println("String = "
                           + str.toString()); 
  
        // replace Character from index 15 to 16 by " not " 
        StringBuilder strReturn = str.replace(15, 16, " not "); 
  
        // print string 
        System.out.println("After Replace() String = "
                           + strReturn.toString()); 
    } 
}

輸出:

String = Tony Stark will die
After Replace() String = Tony Stark will not die

示例3:傳遞負索引時:

// Java program to demonstrate 
// Exception thrown by the replace() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("Tony Stark"); 
  
        try { 
            // replace Character from index -15 to 16 by "Captain America" 
            StringBuilder strReturn = str.replace(-15, 16, "Captain America"); 
        } 
        catch (Exception e) { 
  
            e.printStackTrace(); 
        } 
    } 
}

輸出:

java.lang.StringIndexOutOfBoundsException: String index out of range: -15
    at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:851)
    at java.lang.StringBuilder.replace(StringBuilder.java:262)
    at GFG.main(File.java:17)

示例4:當傳遞的起始索引大於終止索引時:

// Java program to demonstrate 
// Exception thrown by the replace() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuilder object 
        // with a String pass as parameter 
        StringBuilder str 
            = new StringBuilder("Tony Stark"); 
  
        try { 
            // replace Character from index 5 to 3 by "Captain America" 
            StringBuilder strReturn = str.replace(5, 3, "Captain America"); 
        } 
        catch (Exception e) { 
  
            e.printStackTrace(); 
        } 
    } 
}

輸出:

java.lang.StringIndexOutOfBoundsException: start > end
    at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:855)
    at java.lang.StringBuilder.replace(StringBuilder.java:262)
    at GFG.main(File.java:17)

參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#replace(int,int,java.lang.String)



相關用法


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