StringBuffer.replace()是内置方法,用于用指定String中的字符替换此序列的子字符串中的字符。在这里,只是删除了子字符串中的字符,并在开始处插入了其他字符。
用法:
public StringBuffer replace(int first, int last, String st)
参数:该方法接受三个参数。
- first :这是整数类型,表示起始索引。
- last:这是整数类型,表示结束索引。
- st:这是字符串类型,是指将替换先前内容的字符串。
返回值:执行上述操作后,该方法返回此对象。
异常:如果第一个为负,大于length(),或者大于最后一个,则StringIndexOutOfBoundsException。
例子:
Input: StringBuffer= "The first planet of solar system is merrhxy" first = 39 last = 42 st = "cur" Output: The first planet of solar system is mercury
以下程序说明了java.lang.StringBuffer.replace()方法:
示例1:
// Java praogram to illustrate the
// java.lang.StringBuffer.replace()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks");
System.out.println("string buffer = " + sbf);
// Replacing substring from index 15 to index 18
sbf.replace(15, 18, "for");
System.out.println("After replacing string buffer= " + sbf);
}
}
输出:
string buffer = Welcome to Geekshssgeeks After replacing string buffer= Welcome to Geekforsgeeks
示例2:传递负索引时:
// Java praogram to illustrate the
// java.lang.StringBuffer.replace()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks");
System.out.println("string buffer = " + sbf);
// Replacing substring from index -15 to index -18
sbf.replace(-15, -18, "for");
System.out.println("After replacing string buffer= " + sbf);
}
}
输出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -15 at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:851) at java.lang.StringBuffer.replace(StringBuffer.java:452) at Geeks.main(Geeks.java:14)
示例3:当传递的索引大于长度时
// Java praogram to illustrate the
// java.lang.StringBuffer.replace()
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geekshssgeeks");
System.out.println("string buffer = " + sbf);
// Replacing substring from index 215 to index 218
sbf.replace(215, 218, "for");
System.out.println("After replacing string buffer= " + sbf);
}
}
输出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start > length() at java.lang.AbstractStringBuilder.replace(AbstractStringBuilder.java:853) at java.lang.StringBuffer.replace(StringBuffer.java:452) at Geeks.main(Geeks.java:14)
相关用法
- Java StringBuffer deleteCharAt()用法及代码示例
- Java StringBuffer codePointAt()用法及代码示例
- Java StringBuffer setCharAt()用法及代码示例
- Java StringBuffer offsetByCodePoints()用法及代码示例
- Java StringBuffer ensureCapacity()用法及代码示例
- Java StringBuffer delete()用法及代码示例
- Java StringBuffer reverse()用法及代码示例
- Java StringBuffer appendCodePoint()用法及代码示例
- Java StringBuffer getChars()用法及代码示例
- Java StringBuffer substring()用法及代码示例
- Java StringBuffer trimToSize()用法及代码示例
- Java StringBuffer codePointBefore()用法及代码示例
- Java StringBuffer codePointCount()用法及代码示例
- Java StringBuffer indexOf()用法及代码示例
- Java StringBuffer append()用法及代码示例
注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 StringBuffer replace() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。