Java StringBuilder.replace() – Examples
In this tutorial, we will learn about the Java StringBuilder.replace() function, and learn how to use this function to replace a subsequence in StringBuilder with the specified string, with the help of examples.
replace(int start, int end, String str)
StringBuilder.replace() removes characters in of this sequence, that are present in the index range from start
to end
, and then inserts the string at index start
. So, basically, it replaces the subsequence in the StringBuilder with the specified string.
Syntax
The syntax of replace() function is
replace(int start, int end, String str)
where
Parameter | Description |
---|---|
start | The index in this StringBuilder from which replacement will take place. |
end | The index in this StringBuilder up until which replacement will take place. |
str | The string that will replace the subsequence in StringBuilder. |
Returns
The function returns StringBuilder
Example 1 – replace(start, end, str)
In this example, we will take a StringBuilder with sequence "abcdefghijklm"
, delete characters in the ranges [2, 5)
and insert the string "ABCDEF"
at index 2
. So, we are replacing a subsequence with the string.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
System.out.println("Before replace : " + stringBuilder.toString());
int start = 2;
int end = 5;
String str = "ABCDEF";
stringBuilder.replace(start, end, str);
System.out.println("After replace : " + stringBuilder.toString());
}
}
Output
Before replace : abcdefghijklm
After replace : abABCDEFfghijklm
Example 2 – replace(start, end, str) – end > length
In this example, we will give value of end
such that it is greater than length of StringBuilder. replace() deletes all the chars starting from index start
until the length of StringBuilder, and replaces this subsequence with the specified string.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
System.out.println("Before replace : " + stringBuilder.toString());
int start = 2;
int end = 50;
String str = "ABCDEF";
stringBuilder.replace(start, end, str);
System.out.println("After replace : " + stringBuilder.toString());
}
}
Output
Before replace : abcdefghijklm
After replace : abABCDEF
Example 3 – replace(start, end, str) – start < 0
In this example, we will give the index value start
as -2
. Since the start
is out of bounds for the StringBuilder, replace() throws java.lang.StringIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
System.out.println("Before replace : " + stringBuilder.toString());
int start = -2;
int end = 5;
String str = "ABCDEF";
stringBuilder.replace(start, end, str);
System.out.println("After replace : " + stringBuilder.toString());
}
}
Output
Before replace : abcdefghijklm
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start -2, end 5, length 13
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
at java.base/java.lang.AbstractStringBuilder.replace(Unknown Source)
at java.base/java.lang.StringBuilder.replace(Unknown Source)
at Example.main(Example.java:9)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.replace() function, and also learnt how to use this function with the help of examples.