Software Development Exam  >  Software Development Notes  >  Basics of Java  >  StringBuilder Class in Java

StringBuilder Class in Java | Basics of Java - Software Development PDF Download

When working with strings in Java, you may encounter situations where you need to perform various operations like concatenation, appending, or inserting characters. While the String class in Java provides basic functionality for manipulating strings, it is immutable, meaning that every modification creates a new string object. This can be inefficient, especially when dealing with large strings or frequent modifications.

To address this issue, Java provides the StringBuilder class, which offers a more efficient way to manipulate strings. In this article, we will explore the StringBuilder class, its methods, and how to use it effectively.

Creating a StringBuilder Object

To use the StringBuilder class, we first need to create an instance of it. This can be done using the following syntax:

StringBuilder stringBuilder = new StringBuilder();

We can also initialize a StringBuilder object with an initial value by passing a string as a parameter to its constructor:

StringBuilder stringBuilder = new StringBuilder("Hello");

Appending Strings

One of the main advantages of the StringBuilder class is its ability to efficiently append strings. The append() method is used to concatenate or add strings to the existing StringBuilder object. Let's look at an example:

StringBuilder stringBuilder = new StringBuilder("Hello");

stringBuilder.append(", World!");


System.out.println(stringBuilder.toString());

Output

Hello, World!

In the example above, we first create a StringBuilder object with the initial value "Hello". We then use the append() method to add ", World!" to the existing string. Finally, we print the modified string using the toString() method.

Inserting Strings

Apart from appending, the StringBuilder class allows us to insert strings at specific positions within the string. The insert() method is used to achieve this. Let's see an example:

StringBuilder stringBuilder = new StringBuilder("Hello World!");

stringBuilder.insert(6, "Beautiful ");


System.out.println(stringBuilder.toString());

Output

Hello Beautiful World!

In the above example, we have an existing StringBuilder object with the value "Hello World!". We use the insert() method to insert the string "Beautiful " at index 6, resulting in the modified string "Hello Beautiful World!".

Modifying Characters

The StringBuilder class also allows us to modify individual characters within the string. We can use the setCharAt() method to change a character at a specific index. Here's an example:

StringBuilder stringBuilder = new StringBuilder("Hello");

stringBuilder.setCharAt(1, 'a');


System.out.println(stringBuilder.toString());

Output

Hallo

In the above example, we modify the character at index 1 from 'e' to 'a' using the setCharAt() method. The resulting string becomes "Hallo".

Deleting Characters

The StringBuilder class provides methods to delete characters from the string. We can use the delete() method to remove a range of characters or the deleteCharAt() method to remove a specific character at a given index. Let's see an example:

StringBuilder stringBuilder = new StringBuilder("Hello, World!");

stringBuilder.delete(5, 8);


System.out.println(stringBuilder.toString());

Output

Reversing a String

In the example above, we delete the characters from index 5 to 7 using the delete() method. The resulting string becomes "Hello World!".

Reversing a String

The StringBuilder class provides a convenient method to reverse the characters in a string. We can use the reverse() method to achieve this. Here's an example:

StringBuilder stringBuilder = new StringBuilder("Hello");

stringBuilder.reverse();


System.out.println(stringBuilder.toString());

Output

olleH

In the above example, we reverse the characters in the string "Hello" using the reverse() method, resulting in the string "olleH".

Sample Problems

1. Write a program to reverse a given string using StringBuilder and print the result.

String input = "Hello, World!";

StringBuilder stringBuilder = new StringBuilder(input);

stringBuilder.reverse();

String reversed = stringBuilder.toString();


System.out.println(reversed);

Output

!dlroW ,olleH

2. Write a program that takes a sentence as input and prints the sentence with each word reversed.

String sentence = "Hello, how are you?";

String[] words = sentence.split(" ");

StringBuilder stringBuilder = new StringBuilder();


for (String word : words) {

    StringBuilder wordBuilder = new StringBuilder(word);

    wordBuilder.reverse();

    stringBuilder.append(wordBuilder).append(" ");

}


String reversedSentence = stringBuilder.toString().trim();

System.out.println(reversedSentence);

Output

olleH, woh era ?uoy

Conclusion

The StringBuilder class in Java provides a more efficient way to manipulate strings compared to the immutable String class. By using the StringBuilder class, you can easily append, insert, modify, and delete characters within a string without creating unnecessary string objects. This can improve performance and memory usage, especially when dealing with large strings or frequent modifications.

The document StringBuilder Class in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Free

,

shortcuts and tricks

,

MCQs

,

ppt

,

Objective type Questions

,

past year papers

,

study material

,

Extra Questions

,

StringBuilder Class in Java | Basics of Java - Software Development

,

Semester Notes

,

video lectures

,

pdf

,

StringBuilder Class in Java | Basics of Java - Software Development

,

StringBuilder Class in Java | Basics of Java - Software Development

,

mock tests for examination

,

Sample Paper

,

Viva Questions

,

Exam

,

Previous Year Questions with Solutions

,

Important questions

,

practice quizzes

,

Summary

;