In this article, we will discuss StringBuffer’s replace() method which is used to replace portion of string or sub-string with specified string
1. StringBuffer’s replace() method:
- This StringBuffer method is used to replace of portion of string or substring, as indicated by the start-index & end-index with specified string
- Start-index is inclusive whereas end-index is exclusive
- If the specified range (start to end) and specified string’s length is b, then there is no issue
- If the specified range (start to end) is greater than specified string’s length, then specified string will be easily accommodated (and also there will be no white-spaces)
- If the specified range (start to end) is lesser than specified string’s length, then also it will be accommodated
1.1 Method Signature:
public StringBuffer replace(int start, int end, String str);
1.2 Returns:
- Return the same StringBuffer object after replacing specified range (start to end) with specified string
2. Examples on StringBuffer’s replace() method:
- To replace sub-string from invoking StringBuffer object with specified String
StringBufferReplaceMethod.java
package in.bench.resources.stringbuffer.methods;
public class StringBufferReplaceMethod {
public static void main(String[] args) {
// StringBuffer - 1
StringBuffer sb1 = new StringBuffer(
"All humans are Best");
// StringBuffer's length are same
System.out.println("Case 1 : "
+ "both StringBuffer's length are same :\n");
System.out.println("Original StringBuffer : " + sb1);
// Case 1: replace "humans" with "living"
sb1.replace(4, 10, "living");
System.out.println("Replaced StringBuffer : " + sb1);
// StringBuffer - 2
StringBuffer sb2 = new StringBuffer("Test the Best");
// StringBuffer's length is
// greater than replaced string's length
System.out.println("\n\nCase 2 : "
+ "replaced StringBuffer's length is lesser :\n");
System.out.println("Original StringBuffer : " + sb2);
// Case 2: replace "the" with "of" => no white-spaces
sb2.replace(5, 8, "of");
System.out.println("Replaced StringBuffer : " + sb2);
// StringBuffer - 3
StringBuffer sb3 = new StringBuffer("Made in China");
// StringBuffer's length is
// lesser than replaced string's length
System.out.println("\n\nCase 3 : "
+ "replaced StringBuffer's length is greater :\n");
System.out.println("Original StringBuffer : " + sb3);
// Case 3: replace "in" with "with"
sb3.replace(5, 7, "with");
System.out.println("Replaced StringBuffer : " + sb3);
}
}
Output:
Case 1 : both StringBuffer's length are same :
Original StringBuffer : All humans are Best
Replaced StringBuffer : All living are Best
Case 2 : replaced StringBuffer's length is lesser :
Original StringBuffer : Test the Best
Replaced StringBuffer : Test of Best
Case 3 : replaced StringBuffer's length is greater :
Original StringBuffer : Made in China
Replaced StringBuffer : Made with China
Related Articles:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!