StringBuffer

StringBuffer class in Java

StringBuffer is a class in Java that represents a mutable sequence of characters. It provides an alternative to the immutable String class, allowing you to modify the contents of a string without creating a new object every time.

Java Program Demonstrating  Basic StringBuffer Methods

public class StringBufferExample {
public static void main(String[] args) {
// Create a StringBuffer object
StringBuffer sb = new StringBuffer("adib mahfuj");

// Append a string to the end
sb.append(" is a student");
System.out.println("After append: " + sb);

// Insert a string at a specified position
sb.insert(5, "k ");
System.out.println("After insert: " + sb);

// Reverse the contents of the StringBuffer
sb.reverse();
System.out.println("After reverse: " + sb);
sb.reverse(); // Reverse it back to original

// Replace a portion of the string with another string
sb.replace(5, 7, "A");
System.out.println("After replace: " + sb);

// Delete a portion of the string
sb.delete(5, 6);
System.out.println("After delete: " + sb);

// Delete a character at a specified position
sb.deleteCharAt(5);
System.out.println("After deleteCharAt: " + sb);

// Get the current length of the buffer
int length = sb.length();
System.out.println("Length: " + length);

// Get the capacity of the buffer
int capacity = sb.capacity();
System.out.println("Capacity: " + capacity);

// Ensure the capacity is at least a specified minimum
sb.ensureCapacity(50);
System.out.println("Capacity after ensureCapacity: " + sb.capacity());

// Set the length of the buffer
sb.setLength(8);
System.out.println("After setLength: " + sb);

// Get a character at a specified index
char charAt = sb.charAt(2);
System.out.println("Character at index 2: " + charAt);

// Set a character at a specified index
sb.setCharAt(2, 'D');
System.out.println("After setCharAt: " + sb);

// Get a substring from the buffer
String substring = sb.substring(2, 6);
System.out.println("Substring (2, 6): " + substring);

// Convert the buffer to a string
String resultString = sb.toString();
System.out.println("ToString: " + resultString);
}
}

Explanation

  • Creating a StringBuffer Object:
  • StringBuffer sb = new StringBuffer("adib mahfuj");
  • This line creates a StringBuffer object named sb initialized with the string "adib mahfuj". StringBuffer is used to create strings that can be modified.

  • Appending a String:
  • sb.append(" is a student");
  • The append method adds the string " is a student" to the end of the current string. 

  • Inserting a String:
  • sb.insert(5, "k ");
  • The insert method inserts the string "k " at position 5. Positions are zero-based, so position 5 is after "adib ".

  • Reversing the StringBuffer:
  • sb.reverse();  sb.reverse();
  • The reverse method reverses the characters in the StringBuffer. The second reverse call returns it to the original order.

  • Replacing a Portion of the String:
  • sb.replace(5, 7, "A");
  • The replace method replaces the characters from position 5 to 7 with "A".

  • Deleting a Portion of the String:
  • sb.delete(5, 6);
  • The delete method removes the characters from positions 5 to 6.

  • Deleting a Character:
  • sb.deleteCharAt(5);
  • The deleteCharAt method removes the character at position 5.

  • Getting the Length:
  • int length = sb.length();
  • The length method returns the number of characters in the StringBuffer.

  • Getting the Capacity:
  • int capacity = sb.capacity();
  • The capacity method returns the current capacity of the StringBuffer, which is the amount of storage available for new characters without resizing.
  • Ensuring Minimum Capacity:
  • sb.ensureCapacity(50);
  • The ensureCapacity method ensures that the StringBuffer has at least the specified capacity (50 in this case).
  • Setting the Length:
  • sb.setLength(8);
  • The setLength method sets the length of the StringBuffer. If the new length is shorter, the buffer is truncated.
  • Getting a Character:
  • char charAt = sb.charAt(2);
  • The charAt method returns the character at the specified position (index 2).
  • Setting a Character:
  • sb.setCharAt(2, 'D');
  • The setCharAt method sets the character at the specified position (index 2) to 'D'.
  • Getting a Substring:
  • String substring = sb.substring(2, 6);
  • The substring method returns a new string that starts from index 2 and ends at index 6 (excluding 6).
  • Converting to String:
  • String resultString = sb.toString();
  • The toString method converts the StringBuffer to a String.








Comments