🎓 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
In this guide, you will learn about the StringBuilder insert() method in Java programming and how to use it with an example.
1. StringBuilder insert() Method Overview
Definition:
The insert() method in the StringBuilder class is used to insert data into the current sequence at the specified position.
Syntax:
stringBuilder.insert(index, data)
Parameters:
- index: The position at which to insert data.
- data: The data to be inserted.
Key Points:
- The method is overloaded to accept various data types, including char, CharSequence, boolean, int, long, float, double, etc.
- The index refers to the position in the current character sequence where the new data will begin.
- If the index is equal to the length of the current sequence, the data is appended at the end.
- Throws StringIndexOutOfBoundsException if the index is negative or greater than the length of this sequence.
- Inserting a null reference using any of the insert() methods will result in the four characters "null" being inserted.
2. StringBuilder insert() Method Example
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("Hello World");
// Inserting a string at position 5
builder.insert(5, ", dear");
System.out.println(builder); // Outputs: Hello, dear World
// Inserting a character array
char[] charArray = {'!', '!', ' '};
builder.insert(12, charArray);
System.out.println(builder); // Outputs: Hello, dear!! World
// Inserting a boolean value at the end
builder.insert(builder.length(), false);
System.out.println(builder); // Outputs: Hello, dear!! Worldfalse
}
}
Output:
Hello, dear World Hello, dear!! World Hello, dear!! Worldfalse
Explanation:
In this example:
1. We initiate with a StringBuilder object containing the string "Hello World".
2. We use the insert() method to insert the string ", dear" at the 5th position.
3. We insert a character array charArray at the 12th position.
4. Finally, we demonstrate that the insert() method can accept various data types by inserting a boolean value at the end.
The insert() method is versatile due to its multiple overloads that allow for inserting different data types. It provides flexibility and efficiency when adding data to a StringBuilder object at specified positions, eliminating the need for string concatenations.
Related Java StringBuilder class method examples
- Java StringBuilder append() example
- Java StringBuilder delete() example
- Java StringBuilder insert() example
- Java StringBuilder reverse() example
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