Java中的 StringBuffer replace() 方法及示例
StringBuffer.replace() 是内置方法,用于将该序列的子字符串中的字符替换为指定字符串中的字符。这里只是删除子字符串中的字符,并在开头插入其他字符。
句法 :
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() 方法:
方案一:
java
// Java program 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);
}
}
java
// Java program 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);
}
}
java
// Java program 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);
}
}
输出:
string buffer = Welcome to Geekshssgeeks
After replacing string buffer= Welcome to Geekforsgeeks
程序 2:当传递负索引时:
Java
// Java program 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
// Java program 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)