StringBuilder and StringBuffer Methods

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 54:-  StringBuilder and StringBuffer Methods

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:

  1. append(): Adds the specified value to the end of the sequence.

    javaCopy code

    StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("World"); // Result: "Hello World"

  2. insert(): Inserts the specified value at the specified position in the sequence.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello"); sb.insert(5, " World"); // Result: "Hello World"

  3. delete(): Removes the characters between the specified start and end positions.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello, World"); sb.delete(5, 7); // Result: "Hello World"

  4. replace(): Replaces a sequence of characters with the specified value.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello, World"); sb.replace(0, 5, "Hi"); // Result: "Hi, World"

  5. charAt(): Returns the character at the specified index.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello"); char ch = sb.charAt(2); // ch will be 'l'

  6. length(): Returns the length (number of characters) of the sequence.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello"); int length = sb.length(); // length will be 5

  7. substring(): Returns a new string that is a substring of the original sequence.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello, World"); String substring = sb.substring(7); // substring will be "World"

  8. reverse(): Reverses the order of characters in the sequence.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello"); sb.reverse(); // Result: "olleH"

  9. capacity(): Returns the current capacity of the underlying character array.

    javaCopy code

    StringBuilder sb = new StringBuilder("Hello"); int capacity = sb.capacity();

  10. setCharAt(): Replaces the character at the specified index with the specified character.

    javaCopy code

    StringBuilder 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.

9. Strings

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?