Java: StringBuffer replace() method

In this article, we will discuss StringBuffer’s replace() method which is used to replace portion of string or substring with specified string

 

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
  • Case 1: If the specified range (start to end) and specified string’s length is equal, then there is no issue
  • Case 2: 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)
  • Case 3: If the specified range (start to end) is lesser than specified string’s length, then also it will be accommodated

 

Method Signature:

public StringBuffer replace(int start, int end, String str);

 

Returns:

  • Return the same StringBuffer object after replacing specified range (start to end) with specified string

 

Examples on replace() method:

To replace substring 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

 

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/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuffer reverse() method
Java: StringBuffer length() method