Java StringBuilder replace()方法及示例

Java StringBuilder replace()方法及示例

StringBuilder类的 replace(int start, int end, String str) 方法用于用指定的String中的字符替换这个序列的子串中的字符。子串从指定的索引start开始,延伸到索引end-1的字符,如果没有这样的字符,则延伸到序列的末端。首先,子串中的字符被删除,然后插入作为参数传递的字符串,以代替这些字符。

语法

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.lang.String)。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程