🎓 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.toString() method in Java is used to convert a StringBuilder object into a 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.toString() can be used effectively.Table of Contents
- Introduction
toStringMethod Syntax- Examples
- Converting a StringBuilder to a String
- Using toString() After Modifying StringBuilder
- Real-World Use Case
- Example: Building a Dynamic SQL Query
- Conclusion
Introduction
The StringBuilder.toString() method is a member of the StringBuilder class in Java. It converts the contents of the StringBuilder to a String. This method is useful when you need to generate a string from a StringBuilder for output, comparison, or further processing.
toString Method Syntax
The syntax for the toString method is as follows:
public String toString()
- Parameters: None
- Returns: A
Stringrepresenting the data in theStringBuilder.
Examples
Converting a StringBuilder to a String
The toString method can be used to convert the contents of a StringBuilder to a String.
Example
public class ToStringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, world!");
// Converting StringBuilder to String
String str = sb.toString();
// Printing the String
System.out.println("String: " + str);
}
}
Output:
String: Hello, world!
Using toString() After Modifying StringBuilder
You can modify the StringBuilder and then use the toString method to get the updated string.
Example
public class ModifyAndToStringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
// Modifying the StringBuilder
sb.append(", world!");
// Converting StringBuilder to String
String str = sb.toString();
// Printing the String
System.out.println("String: " + str);
}
}
Output:
String: Hello, world!
Real-World Use Case
Example: Building a Dynamic SQL Query
A common real-world use case for StringBuilder.toString() is building a dynamic SQL query.
Example
public class SQLQueryBuilder {
public static void main(String[] args) {
String tableName = "users";
StringBuilder queryBuilder = new StringBuilder();
// Building the SQL query using StringBuilder
queryBuilder.append("SELECT * FROM ").append(tableName).append(" WHERE ");
String[] columns = {"name", "age", "city"};
String[] values = {"'John'", "30", "'New York'"};
for (int i = 0; i < columns.length; i++) {
queryBuilder.append(columns[i]).append(" = ").append(values[i]);
if (i < columns.length - 1) {
queryBuilder.append(" AND ");
}
}
// Converting StringBuilder to String
String query = queryBuilder.toString();
// Printing the SQL query
System.out.println("SQL Query: " + query);
}
}
Output:
SQL Query: SELECT * FROM users WHERE name = 'John' AND age = 30 AND city = 'New York'
In this example, StringBuilder.toString() is used to generate a dynamic SQL query, demonstrating how it can be useful for creating complex strings from multiple parts.
Conclusion
The StringBuilder.toString() method in Java provides a way to convert the contents of a StringBuilder object into a String. By understanding how to use this method, you can efficiently generate strings from StringBuilder objects in your Java applications. The method allows you to take advantage of the performance benefits of StringBuilder for string manipulation and then convert the result into a String for further use.
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