6.B] Explain all String Buffer methods with examples.
Answer:
The StringBuffer
class in Java is used to create mutable (modifiable) strings.
Unlike String
, which is immutable, StringBuffer
allows strings to be changed without creating new objects. Below are some of the commonly used StringBuffer
methods along with examples.
1. append(String str)
- Purpose: Adds the specified string to the end of the current
StringBuffer
. - Syntax:
sb.append(str);
- Example:
StringBuffer sb = new StringBuffer("Hello"); sb.append(" World"); System.out.println(sb); // Output: Hello World
2. insert(int offset, String str)
- Purpose: Inserts the specified string at the given offset (position) in the
StringBuffer
. - Syntax:
sb.insert(offset, str);
- Example:
StringBuffer sb = new StringBuffer("Hello World"); sb.insert(6, "Beautiful "); System.out.println(sb); // Output: Hello Beautiful World
3. replace(int start, int end, String str)
- Purpose: Replaces the characters in the substring of this
StringBuffer
starting at the specified start index and ending at the end index with the specified string. - Syntax:
sb.replace(start, end, str);
- Example:
StringBuffer sb = new StringBuffer("Hello World"); sb.replace(6, 11, "Java"); System.out.println(sb); // Output: Hello Java
4. delete(int start, int end)
- Purpose: Removes the characters in the substring from the start index to the end index.
- Syntax:
sb.delete(start, end);
- Example:
StringBuffer sb = new StringBuffer("Hello World"); sb.delete(5, 11); System.out.println(sb); // Output: Hello
5. deleteCharAt(int index)
- Purpose: Removes the character at the specified position in the
StringBuffer
. - Syntax:
sb.deleteCharAt(index);
- Example:
StringBuffer sb = new StringBuffer("Hello"); sb.deleteCharAt(4); System.out.println(sb); // Output: Hell
6. reverse()
- Purpose: Reverses the sequence of characters in the
StringBuffer
. - Syntax:
sb.reverse();
- Example:
StringBuffer sb = new StringBuffer("Hello"); sb.reverse(); System.out.println(sb); // Output: olleH
7. setCharAt(int index, char ch)
- Purpose: Sets the character at the specified index to the given character.
- Syntax:
sb.setCharAt(index, ch);
- Example:
StringBuffer sb = new StringBuffer("Hello"); sb.setCharAt(1, 'a'); System.out.println(sb); // Output: Hallo
8. setLength(int newLength)
- Purpose: Sets the length of the
StringBuffer
. If the new length is greater than the current length, null characters (\u0000
) are added. If it’s less, the string is truncated. - Syntax:
sb.setLength(newLength);
- Example:
StringBuffer sb = new StringBuffer("Hello"); sb.setLength(2); System.out.println(sb); // Output: He
9. capacity()
- Purpose: Returns the current capacity of the
StringBuffer
(the amount of storage available for new characters without resizing). - Syntax:
int capacity = sb.capacity();
- Example:
StringBuffer sb = new StringBuffer(); System.out.println(sb.capacity()); // Output: 16 (default capacity)
10. ensureCapacity(int minimumCapacity)
- Purpose: Ensures that the
StringBuffer
has at least the specified capacity. - Syntax:
sb.ensureCapacity(minimumCapacity);
- Example:
StringBuffer sb = new StringBuffer(); sb.ensureCapacity(30); System.out.println(sb.capacity()); // Output: 30
11. length()
- Purpose: Returns the number of characters stored in the
StringBuffer
. - Syntax:
int len = sb.length();
- Example:
StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb.length()); // Output: 5
12. substring(int start)
and substring(int start, int end)
- Purpose: Returns a new string that contains a subsequence of characters currently contained in the
StringBuffer
. The substring begins at the specified index and extends to the end or specified end index. - Syntax:
String subStr = sb.substring(start);
String subStr = sb.substring(start, end);
- Example:
StringBuffer sb = new StringBuffer("Hello World"); String subStr = sb.substring(6); System.out.println(subStr); // Output: World
Summary:
append()
: Adds text at the end.insert()
: Inserts text at a specific position.replace()
: Replaces a portion of the string.delete()
anddeleteCharAt()
: Removes text or a specific character.reverse()
: Reverses the string.setCharAt()
: Changes a specific character.setLength()
: Adjusts the length of the string.capacity()
andensureCapacity()
: Deals with the buffer’s capacity.length()
: Returns the current length.substring()
: Extracts part of the string.