0% found this document useful (0 votes)
9 views2 pages

Java StringBuffer Methods Explained

The document is a Java program demonstrating various methods of the StringBuffer class. It showcases operations such as appending, inserting, replacing, deleting, reversing, and manipulating the capacity and length of a StringBuffer object. Each method is illustrated with examples and output statements to show the results of the operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java StringBuffer Methods Explained

The document is a Java program demonstrating various methods of the StringBuffer class. It showcases operations such as appending, inserting, replacing, deleting, reversing, and manipulating the capacity and length of a StringBuffer object. Each method is illustrated with examples and output statements to show the results of the operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

public class StringBufferMethodsDemo {

public static void main(String[] args) {

// 1. Creating a StringBuffer object

StringBuffer sb = new StringBuffer("Hello");

// 2. append() - Adds text at the end

[Link](" Java");

[Link]("Append: " + sb);

// 3. insert() - Inserts text at a specific position

[Link](6, "World ");

[Link]("Insert: " + sb);

// 4. replace() - Replaces characters in a specific range

[Link](6, 11, "Beautiful");

[Link]("Replace: " + sb);

// 5. delete() - Deletes characters in a specific range

[Link](6, 15);

[Link]("Delete: " + sb);

// 6. deleteCharAt() - Deletes a character at a specific index

[Link](5);

[Link]("DeleteCharAt: " + sb);

// 7. reverse() - Reverses the string

[Link]();

[Link]("Reverse: " + sb);

[Link](); // Reverting back to original for further operations

// 8. capacity() - Returns the current capacity

[Link]("Capacity: " + [Link]());

// 9. ensureCapacity() - Ensures minimum capacity

[Link](50);

[Link]("Capacity after ensuring 50: " + [Link]());

// 10. length() - Returns the length of the string

[Link]("Length: " + [Link]());

// 11. charAt() - Returns a character at a specific index


[Link]("Char at index 1: " + [Link](1));

// 12. setCharAt() - Sets a character at a specific index

[Link](1, 'a');

[Link]("SetCharAt: " + sb);

// 13. substring(start) - Extracts substring from start index

[Link]("Substring from index 3: " + [Link](3));

// 14. substring(start, end) - Extracts substring from start to end-1

[Link]("Substring (0,5): " + [Link](0, 5));

// 15. indexOf() - Returns index of first occurrence of a string

[Link]("Index of 'Java': " + [Link]("Java"));

// 16. lastIndexOf() - Returns index of last occurrence of a string

[Link](" Java is fun");

[Link]("Last index of 'Java': " + [Link]("Java"));

// 17. setLength() - Sets the length of the string

[Link](5);

[Link]("After setLength(5): " + sb);

Common questions

Powered by AI

The substring method extracts a part of the StringBuffer content without modifying the actual buffer itself. There are two versions: one that takes a single start index and another that takes both start and end indices. In 'sb.substring(3)', "lo va" is extracted from "Hello va", ignoring initial characters. Similarly, 'sb.substring(0,5)' extracts "Hello", demonstrating how different integer parameters define substring boundaries in StringBuffer content .

The delete() method removes characters from a specified start index to an end index (exclusive) within a StringBuffer, thus altering its content and reducing its length. In the source, sb.delete(6, 15) is used to remove characters between indices 6 and 15 ("WorldJa") from "Hello World Java", leaving "Hello va" .

The indexOf() method returns the index of the first occurrence of a specified substring within a StringBuffer, while lastIndexOf() finds the last occurrence. In the source, 'sb.indexOf("Java")' returns 6 when "Hello Java" is searched. After appending "Java is fun", 'sb.lastIndexOf("Java")' captures the occurrence at a new position, which is 11, demonstrating their utility in locating substrings within the buffer content .

The replace() method replaces characters in a specified range within a StringBuffer with a new string, modifying its content and potentially its length. In the provided example, the range from index 6 to 11 in "Hello Java" is replaced with "Beautiful", resulting in "Hello Beautiful Java" .

The setLength() method sets the length of the character sequence in a StringBuffer. If the new length is greater than the current length, the buffer is padded with null characters ('\0'). If it is less, the buffer is truncated to the new length. In the source, 'sb.setLength(5)' reduces the content of "Hello va" to "Hello", shortening its length effectively .

The reverse() method reverses the sequence of characters in a StringBuffer, effectively altering its content by reversing its order. In the source, sb.reverse() is called when 'sb' holds "Hello va", resulting in "av olleH". After showcasing the reverse method, the buffer is reversed again to revert back to the original order for further operations .

The insert() method allows insertion of a specified string at a specified index within a StringBuffer. It alters the content by shifting the existing characters to accommodate the new string. In the source example, sb.insert(6, "World ") is used to insert "World " at index 6, changing the buffer from "Hello Java" to "Hello World Java" .

The append() method adds text to the end of a StringBuffer object. This method increases the length of the string contained in the buffer by the number of characters being appended. In the source, the StringBuffer object 'sb' initially holds the string "Hello". When sb.append(" Java") is called, the string " Java" is added to the end, resulting in "Hello Java" .

The setCharAt() method updates the character at a specific index within a StringBuffer. In the document, this method alters the character at index 1. For example, from "Hello va", 'sb.setCharAt(1, 'a')' changes the 'e' to 'a', resulting in "Hallo va" .

The ensureCapacity() method ensures that the capacity of the StringBuffer is at least equal to the specified minimum capacity. It is used to optimize the performance of appending large amounts of data by pre-allocating enough memory. In the provided source, the initial capacity is checked using sb.capacity(), then sb.ensureCapacity(50) is called to ensure at least 50 characters can be held, and the capacity is updated if it was initially lower .

You might also like