Software Development Exam  >  Software Development Notes  >  Basics of Java  >  String Buffer Class in Java

String Buffer Class in Java | Basics of Java - Software Development PDF Download

In Java, the StringBuffer class is used to create and manipulate mutable (modifiable) sequences of characters. It provides a convenient way to modify strings without creating new objects, which can be useful in scenarios where frequent string modifications are required. This article will introduce you to the StringBuffer class, explain its features, and provide examples to help you understand how to use it effectively.

Creating a StringBuffer Object

To create a StringBuffer object, you can use the new keyword followed by the StringBuffer class name and parentheses. Here's an example:

StringBuffer sb = new StringBuffer();

This creates an empty StringBuffer object that can be used to store characters.

Appending Characters and Strings

One of the main features of the StringBuffer class is the ability to append characters and strings to the existing sequence. This can be done using the append() method. Let's look at an example:

StringBuffer sb = new StringBuffer();

sb.append("Hello");

sb.append(" World!");


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

Output

Hello World!

In the above example, we create a StringBuffer object and append the strings "Hello" and " World!" to it using the append() method. The toString() method is used to convert the StringBuffer object to a string and then printed to the console.

Inserting Characters and Strings

The StringBuffer class also allows you to insert characters and strings at a specified position within the sequence. This can be done using the insert() method. Here's an example:

StringBuffer sb = new StringBuffer("Hello!");


sb.insert(5, " World");


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

Output

Hello World!

In the above example, we create a StringBuffer object with the initial string "Hello!". Then, we use the insert() method to insert the string " World" at the 5th position within the sequence.

Deleting Characters

The StringBuffer class provides methods to delete characters from the sequence. You can delete a single character using the deleteCharAt() method or delete a range of characters using the delete() method. Let's see some examples:

StringBuffer sb = new StringBuffer("Hello World!");


sb.deleteCharAt(6);

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

Output

HelloWorld!

In the above example, we create a StringBuffer object with the string "Hello World!". Then, we use the deleteCharAt() method to delete the character at index 6, which is the space character.

StringBuffer sb = new StringBuffer("Hello World!");


sb.delete(6, 11);

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

Output

Hello!

In the above example, we use the delete() method to delete the characters in the range from index 6 to 11, which removes the substring "World".

Modifying Characters

The StringBuffer class also allows you to modify individual characters within the sequence. You can do this using the setCharAt() method. Here's an example:

StringBuffer sb = new StringBuffer("Hello!");


sb.setCharAt(4, ',');

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

Output

Hell,!

In the above example, we use the setCharAt() method to change the character at index 4 to a comma (',').

Sample Problems

Problem 1: Write a program to reverse a string using StringBuffer.

String str = "Hello World!";

StringBuffer sb = new StringBuffer(str);


sb.reverse();

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

Output

!dlroW olleH

Problem 2: Write a program to remove all the vowels from a given string using StringBuffer.

String str = "Hello World!";

StringBuffer sb = new StringBuffer(str);


for (int i = 0; i < sb.length(); i++) {

    char ch = sb.charAt(i);

    if (isVowel(ch)) {

        sb.deleteCharAt(i);

        i--; // Adjust the index after deletion

    }

}


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

Output

Hll Wrld!

In the above solution, we define a helper method isVowel() to check if a character is a vowel. We iterate through the characters of the StringBuffer object, delete the vowel characters using the deleteCharAt() method, and adjust the index accordingly.

Conclusion

The StringBuffer class in Java provides a flexible and efficient way to manipulate mutable sequences of characters. It offers methods to append, insert, delete, and modify characters within the sequence. By using the StringBuffer class, you can easily perform string manipulations without creating multiple string objects, which can improve performance.

The document String Buffer 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

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

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
Related Searches

Viva Questions

,

Important questions

,

MCQs

,

mock tests for examination

,

String Buffer Class in Java | Basics of Java - Software Development

,

practice quizzes

,

video lectures

,

Exam

,

String Buffer Class in Java | Basics of Java - Software Development

,

Objective type Questions

,

past year papers

,

Free

,

shortcuts and tricks

,

ppt

,

pdf

,

study material

,

Previous Year Questions with Solutions

,

String Buffer Class in Java | Basics of Java - Software Development

,

Extra Questions

,

Sample Paper

,

Semester Notes

,

Summary

;