The StringBuilder.replace()
method in Java is used to replace a sequence of characters within a StringBuilder
object with another string.
Table of Contents
- Introduction
replace
Method Syntax- Examples
- Replacing a Substring
- Replacing Characters in a Specified Range
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.replace()
method is a member of the StringBuilder
class in Java. It allows you to replace a subsequence of characters within the StringBuilder
with another string. This method is particularly useful when you need to modify specific parts of the string efficiently.
replace() Method Syntax
The syntax for the replace
method is as follows:
public StringBuilder replace(int start, int end, String str)
- start: The beginning index, inclusive.
- end: The ending index, exclusive.
- str: The string that will replace the specified subsequence.
Examples
Replacing a Substring
The replace
method can be used to replace a substring within a StringBuilder
.
Example
public class StringBuilderReplaceExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
// Replace "World" with "Java"
sb.replace(7, 12, "Java");
// Print the result
System.out.println("StringBuilder after replace: " + sb.toString());
}
}
Output:
StringBuilder after replace: Hello, Java!
Replacing Characters in a Specified Range
You can also replace characters within a specified range with another string.
Example
public class StringBuilderReplaceExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abcdefg");
// Replace characters from index 2 to 5 with "12345"
sb.replace(2, 5, "12345");
// Print the result
System.out.println("StringBuilder after replace: " + sb.toString());
}
}
Output:
StringBuilder after replace: ab12345fg
Real-World Use Case
Example: Censoring Sensitive Information
In a real-world scenario, you might need to censor sensitive information in a string. Using the replace
method, you can replace specific words or characters with asterisks or other symbols.
Example Code
public class SensitiveInfoCensor {
public static void main(String[] args) {
StringBuilder message = new StringBuilder("The password is 12345 and the pin is 6789.");
// Replace "12345" with "*****"
int startPassword = message.indexOf("12345");
if (startPassword != -1) {
message.replace(startPassword, startPassword + 5, "*****");
}
// Replace "6789" with "****"
int startPin = message.indexOf("6789");
if (startPin != -1) {
message.replace(startPin, startPin + 4, "****");
}
// Print the censored message
System.out.println("Censored message: " + message.toString());
}
}
Output:
Censored message: The password is ***** and the pin is ****.
Conclusion
The StringBuilder.replace()
method in Java is used for modifying specific parts of a string within a StringBuilder
object. By understanding how to use this method, you can efficiently replace substrings and characters within your strings. Whether you need to replace a substring, handle sensitive information, or modify characters within a specified range, the replace
method provides a reliable solution for these tasks.