Table of contents | |
Creating a StringBuffer Object | |
Appending Characters and Strings | |
Inserting Characters and Strings | |
Deleting Characters | |
Modifying Characters | |
Sample Problems | |
Conclusion |
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.
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.
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.
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.
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".
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 (',').
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.
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.
60 videos|37 docs|12 tests
|
|
Explore Courses for EmSAT Achieve exam
|