Java: StringBuilder replace() method

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

 

replace() method:

  • This StringBuilder method is used to replace of portion of string or sub-string as indicated by the start-index and 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 StringBuilder replace(int start, int end, String str);

Returns:

  • Returns same StringBuilder object after replacing specified range (start to end) with specified string

 

Examples on replace() method:

To replace substring from invoking StringBuilder object with specified String

StringBuilderReplaceMethod.java

package in.bench.resources.stringbuilder.methods;

public class StringBuilderReplaceMethod {

	public static void main(String[] args) {

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder(
				"All robots are Best");

		// StringBuilder's length are same
		System.out.println("Case 1 : "
				+ "both StringBuilder's length are same :\n");
		System.out.println("Original StringBuilder : " + sb1);

		// Case 1: replace "robots" with "living"
		sb1.replace(4, 10, "living");
		System.out.println("Replaced StringBuilder : " + sb1);


		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder("Kill the sarcasm");

		// StringBuilder's length is 
		// greater than replaced string's length
		System.out.println("\n\nCase 2 : "
			   + "replaced StringBuilder's length is lesser :\n");
		System.out.println("Original StringBuilder : " + sb2);

		// Case 2: replace "the" with "of" => no white-spaces
		sb2.replace(5, 8, "of");
		System.out.println("Replaced StringBuilder : " + sb2);


		// StringBuilder - 3
		StringBuilder sb3 = new StringBuilder("Made in USA");

		// StringBuilder's length is 
		// lesser than replaced string's length
		System.out.println("\n\nCase 3 : "
			  + "replaced StringBuilder's length is greater :\n");
		System.out.println("Original StringBuilder : " + sb3);

		// Case 3: replace "in" with "with"
		sb3.replace(5, 7, "with");
		System.out.println("Replaced StringBuilder : " + sb3);
	}
}

Output:

Case 1 : both StringBuilder's length are same :

Original StringBuilder : All robots are Best
Replaced StringBuilder : All living are Best


Case 2 : replaced StringBuilder's length is lesser :

Original StringBuilder : Kill the sarcasm
Replaced StringBuilder : Kill of sarcasm


Case 3 : replaced StringBuilder's length is greater :

Original StringBuilder : Made in USA
Replaced StringBuilder : Made with USA

 

References:

 

Happy Coding !!
Happy Learning !!

Java: StringBuilder reverse() method
Java: StringBuilder length() method