📜  Java中的 StringBuilder replace() 示例

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

Java中的 StringBuilder replace() 示例

StringBuilder类的replace(int start, int end, String str)方法用于将该序列的子串中的字符替换为指定String中的字符。子字符串从指定的索引 start 开始并延伸到索引 end – 1 处的字符,如果不存在这样的字符,则延伸到序列的末尾。首先,删除 substring 的字符,并插入作为参数传递的字符串来代替这些字符。
句法:

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

参数:
该方法接受三个参数:

  1. start -- 整数类型值,它指的是起始索引。
  2. end -- 表示结束索引的整数类型值。
  3. str – 字符串类型值,它引用将替换先前内容的字符串。

回报:
此方法在字符替换成功后返回 StringBuilder 对象。
例外:
如果 start 为负数、大于 length() 或大于 end,则 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 Java)