Java — Important Methods of StringBuffer Class | Code Factory

Code Factory
3 min readMay 16, 2020

--

Index Page : Link

Donate : Link

1. public int length()

- Returns the length (character count).

StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.length()); // 4

2. public int capacity()

- Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.capacity()); // 20

3. public char charAt(int index)

- Returns the char value in this sequence at the specified index. The first char value is at index 0, the next at index 1, and so on, as in array indexing.

- IndexOutOfBoundsException - if index is negative or greater than or equal to length().

StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.charAt(0)); // C
System.out.println(sb.charAt(5)); // RE : java.lang.StringIndexOutOfBoundsException: String index out of range: 5

4. public void setCharAt(int index, char ch)

- The character at the specified index is set to ch. This sequence is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the character ch at position index.

- IndexOutOfBoundsException - if index is negative or greater than or equal to length().

StringBuffer sb = new StringBuffer("Code");
sb.setCharAt(3, 'o');
System.out.println(sb); // Codo

5. append

- StringBuffer contains multiple methods with append name.

1. public StringBuffer append(boolean b)
2. public StringBuffer append(char c)
3. public StringBuffer append(char[] str)
4. public StringBuffer append(char[] str, int offset, int len)
5. public StringBuffer append(CharSequence s)
6. public StringBuffer append(CharSequence s, int start, int end)
7. public StringBuffer append(double d)
8. public StringBuffer append(float f)
9. public StringBuffer append(int i)
10. public StringBuffer append(long lng)
11. public StringBuffer append(Object obj)
12. public StringBuffer append(String str)
13. public StringBuffer append(StringBuffer sb)
StringBuffer sb = new StringBuffer("Code");
sb.append(123);
System.out.println(sb); // Code123

6. insert

- StringBuffer contains multiple methods with insert name.

1. public StringBuffer insert(int offset, boolean b)
2. public StringBuffer insert(int offset, char c)
3. public StringBuffer insert(int offset, char[] str)
4. public StringBuffer insert(int index, char[] str, int offset, int len)
5. public StringBuffer insert(int dstOffset, CharSequence s)
6. public StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
7. public StringBuffer insert(int offset, double d)
8. public StringBuffer insert(int offset, float f)
9. public StringBuffer insert(int offset, int i)
10. public StringBuffer insert(int offset, long l)
11. public StringBuffer insert(int offset, Object obj)
12. public StringBuffer insert(int offset, String str)
StringBuffer sb = new StringBuffer("Code");
sb.insert(0, 123);
System.out.println(sb); // 123Code

7. public StringBuffer delete(int start, int end)

- Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.

- StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

StringBuffer sb = new StringBuffer("Code");
sb.delete(3, 4);
System.out.println(sb); // Cod

8. public StringBuffer deleteCharAt(int index)

- Removes the char at the specified position in this sequence. This sequence is shortened by one char.

- StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length().

StringBuffer sb = new StringBuffer("Code");
sb.deleteCharAt(3);
System.out.println(sb); // Cod

9. public StringBuffer reverse()

- Causes this character sequence to be replaced by the reverse of the sequence.

StringBuffer sb = new StringBuffer("Code");
System.out.println(sb.reverse()); // edoC

10. public void setLength(int newLength)

- Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument.

- IndexOutOfBoundsException - if the newLength argument is negative.

StringBuffer sb = new StringBuffer("CodeFactory");
sb.setLength(4);
System.out.println(sb); // Code

11. public void ensureCapacity(int minimumCapacity)

- Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity.

StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // 16
sb.ensureCapacity(50);
System.out.println(sb.capacity()); // 50

12. public void trimToSize()

- Attempts to reduce storage used for the character sequence. If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to become more space efficient.

StringBuffer sb = new StringBuffer(50);
sb.append("Code");
System.out.println(sb.capacity()); // 50
sb.trimToSize();
System.out.println(sb.capacity()); // 4

--

--