If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Both StringBuilder
and StringBuffer
are classes in Java that provide similar methods for creating and manipulating mutable sequences of characters. These methods allow you to modify strings efficiently, especially in scenarios where multiple modifications are performed sequentially. Here are some of the commonly used methods of StringBuilder
and StringBuffer
:
append()
: Adds the specified value to the end of the sequence.
javaCopy codeStringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
// Result: "Hello World"
insert()
: Inserts the specified value at the specified position in the sequence.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
sb.insert(5, " World");
// Result: "Hello World"
delete()
: Removes the characters between the specified start and end positions.
javaCopy codeStringBuilder sb = new StringBuilder("Hello, World");
sb.delete(5, 7);
// Result: "Hello World"
replace()
: Replaces a sequence of characters with the specified value.
javaCopy codeStringBuilder sb = new StringBuilder("Hello, World");
sb.replace(0, 5, "Hi");
// Result: "Hi, World"
charAt()
: Returns the character at the specified index.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
char ch = sb.charAt(2);
// ch will be 'l'
length()
: Returns the length (number of characters) of the sequence.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
int length = sb.length();
// length will be 5
substring()
: Returns a new string that is a substring of the original sequence.
javaCopy codeStringBuilder sb = new StringBuilder("Hello, World");
String substring = sb.substring(7);
// substring will be "World"
reverse()
: Reverses the order of characters in the sequence.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
sb.reverse();
// Result: "olleH"
capacity()
: Returns the current capacity of the underlying character array.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
int capacity = sb.capacity();
setCharAt()
: Replaces the character at the specified index with the specified character.
javaCopy codeStringBuilder sb = new StringBuilder("Hello");
sb.setCharAt(0, 'J');
// Result: "Jello"
These are just a few examples of the methods available in StringBuilder
and StringBuffer
. Both classes provide a rich set of methods that allow you to efficiently manipulate strings as needed in your application. Remember that StringBuilder
is not thread-safe, whereas StringBuffer
is thread-safe but might be slightly slower due to the added synchronization overhead. Choose the appropriate class based on your specific requirements.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0