In this article, we will discuss StringBuilder’s replace() method which is used to replace portion of string or sub-string with specified string
1. StringBuilder’s 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
1.1 Method Signature:
public StringBuilder replace(int start, int end, String str);
1.2 Returns:
- Returns same StringBuilder object after replacing specified range (start to end) with specified string
2. Examples on replace() method :
2.1 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
Related Articles :
- Java – StringBuilder class
- Java – StringBuilder append() method (13)
- Java – StringBuilder capacity() method
- Java – StringBuilder charAt(int index) method
- Java – StringBuilder delete(int start, int end) method
- Java – StringBuilder deleteCharAt(int index) method
- Java – StringBuilder ensureCapacity(int minimumCapacity) method
- Java – StringBuilder indexOf() method (2)
- Java – StringBuilder insert() method (12)
- Java – StringBuilder lastIndexOf() method (2)
- Java – StringBuilder length() method
- Java – StringBuilder replace(int start, int end, String str) method
- Java – StringBuilder reverse() method
- Java – StringBuilder 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 !!