🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
StringBuilder.replace() method in Java is used to replace a sequence of characters in a StringBuilder object with another string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how StringBuilder.replace() can be used effectively.Table of Contents
- Introduction
replaceMethod Syntax- Examples
- Replacing a Substring in a StringBuilder
- Replacing a Substring with an Empty String
- Real-World Use Case
- Example: Censoring Sensitive Information
- Conclusion
Introduction
The StringBuilder.replace() method is a member of the StringBuilder class in Java. It allows you to replace a sequence of characters between specified start and end indices with another string. This method is useful for modifying portions of a string within a StringBuilder object.
replace Method Syntax
The syntax for the replace method is as follows:
public StringBuilder replace(int start, int end, String str)
- Parameters:
start: The beginning index, inclusive.end: The ending index, exclusive.str: The string that will replace the specified sequence of characters.
- Returns: A reference to the same
StringBuilderobject, with the specified sequence of characters replaced by the new string.
Examples
Replacing a Substring in a StringBuilder
The replace method can be used to replace a specific substring within a StringBuilder object.
Example
public class ReplaceExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, world!");
// Replacing the substring "world" with "Java"
sb.replace(7, 12, "Java");
// Printing the modified StringBuilder
System.out.println(sb.toString());
}
}
Output:
Hello, Java!
Replacing a Substring with an Empty String
You can replace a substring with an empty string, effectively removing that substring from the StringBuilder.
Example
public class RemoveSubstringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, world!");
// Removing the substring "world"
sb.replace(7, 12, "");
// Printing the modified StringBuilder
System.out.println(sb.toString());
}
}
Output:
Hello, !
Real-World Use Case
Example: Censoring Sensitive Information
A common real-world use case for StringBuilder.replace() is censoring sensitive information in a text, such as replacing certain words with asterisks.
Example
public class CensorSensitiveInfo {
public static void main(String[] args) {
StringBuilder text = new StringBuilder("This is a confidential document. Do not share this document.");
// Censoring the word "confidential"
int start = text.indexOf("confidential");
int end = start + "confidential".length();
text.replace(start, end, "***********");
// Printing the censored text
System.out.println(text.toString());
}
}
Output:
This is a *********** document. Do not share this document.
In this example, StringBuilder.replace() is used to censor the word "confidential" by replacing it with asterisks, demonstrating how it can be useful for modifying text to hide sensitive information.
Conclusion
The StringBuilder.replace() method in Java provides a way to replace a sequence of characters within a StringBuilder object with another string. By understanding how to use this method, you can efficiently modify portions of strings in your Java applications. The method allows you to perform various string replacement operations, making it a versatile tool for string manipulation in various scenarios.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment