Java StringBuffer Methods Explained
Java StringBuffer Methods Explained
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 .