Table of contents | |
Appending Strings | |
Inserting Strings | |
Modifying Characters | |
Deleting Characters | |
Reversing a String | |
Sample Problems | |
Conclusion |
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.
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");
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.
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!".
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".
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!".
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".
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
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.
60 videos|37 docs|12 tests
|
|
Explore Courses for Software Development exam
|