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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。