🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this blog post, we will learn what is StringBuilder, key points about StringBuilder, important methods with examples, performance tips, and when to use StringBuilder in Java.
Introduction to StringBuilder
StringBuilder is a mutable sequence of characters that is more efficient than using String for string manipulations. It is preferred in situations where you have to make several modifications to your string content.Key points about StringBuilder:
- append(): Add characters or other data types to the end.
- insert(): Insert characters at a specific index.
- delete(): Remove characters within a specific range.
- reverse(): Reverse the order of characters.
- toString(): Convert to a standard string.
Important Methods of StringBuilder
1. append
StringBuilder builder = new StringBuilder();
builder.append("Java");
builder.append("Guides");2. insert
builder.insert(4, " "); // Inserts space between Java and Guides3. delete
builder.delete(0, 4); // Removes the word "Java"4. reverse
builder.reverse(); // Reverses the content5. length
int len = builder.length(); // Gets the length6. setLength
builder.setLength(5); // Sets the length to 57. replace(int start, int end, String str)
StringBuilder builder = new StringBuilder("JavaGuides");
builder.replace(4, 5, " Guides");
// Result: "Java Guides"8. charAt(int index)
char ch = builder.charAt(5);
// Result: 'G'9. setCharAt(int index, char ch)
builder.setCharAt(5, 'g');
// Result: "Java guides"10. substring(int start) and substring(int start, int end)
String sub1 = builder.substring(5);
// Result: "guides"
String sub2 = builder.substring(5, 11);
// Result: "guides"11. indexOf(String str) and lastIndexOf(String str)
int firstIndex = builder.indexOf("g");
// Result: 5
int lastIndex = builder.lastIndexOf("g");
// Result: 512. deleteCharAt(int index)
builder.deleteCharAt(5);
// Result: "Java uides"Performance Tips with StringBuilder
1. Prefer StringBuilder Over String Concatenation
2. Initialize with Appropriate Capacity
StringBuilder builder = new StringBuilder(50);3. Reuse StringBuilder Instances
4. Prefer StringBuilder if Synchronization is Not Required
When to use StringBuilder?
Conclusion
Related Blog Posts
- Java StringBuffer: Methods, Examples, and Performance Tips
- Java StringBuffer Class API Guide
- String vs StringBuffer in Java with Example (Performance Analysis)
- Java StringBuilder: Basics, Methods, Examples, Performance Tips
- Java String: A Guide to String Basics, Methods, Immutability, Performance, and Best Practices
- Java String Class API Guide - Covers all the String Methods
- Best Way to Reverse a String in Java
- Guide to Java String Constant Pool
- Guide to String Best Practices in Java (Best Practice)
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