Java – StringBuffer replace() method

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
    1. If the specified range (start to end) and specified string’s length is b, then there is no issue
    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)
    3. 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:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer reverse() method
Java - StringBuffer length() method