StringBuilder类的replace(int start,int end,String str)方法用于用指定String中的字符替换此序列的子字符串中的字符。子字符串从指定的索引开头开始,并扩展到索引结尾– 1处的字符,或者扩展到序列末尾(如果不存在这样的字符)。首先,删除子字符串的字符,并插入作为参数传递的字符串来代替这些字符。
用法:
public StringBuilder replace?(int start, int end, String str)
参数:
此方法接受三个参数:
- start–引用起始索引的整数类型值。
- end–引用结束索引的整数类型值。
- 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)
相关用法
- Java StringBuilder reverse()用法及代码示例
- Java StringBuilder codePointAt()用法及代码示例
- Java StringBuilder length()用法及代码示例
- Java StringBuilder delete()用法及代码示例
- Java StringBuilder charAt()用法及代码示例
- Java StringBuilder deleteCharAt()用法及代码示例
- Java StringBuilder setCharAt()用法及代码示例
- Java StringBuilder ensureCapacity()用法及代码示例
- Java StringBuilder getChars()用法及代码示例
- Java StringBuilder subSequence()用法及代码示例
- Java StringBuilder codePointCount()用法及代码示例
- Java StringBuilder setLength()用法及代码示例
- Java StringBuilder capacity()用法及代码示例
- Java StringBuilder codePointBefore()用法及代码示例
- Java StringBuilder Class用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder replace() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。