Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Java StringBuffer Class



StringBuffer Class Introduction

The Java StringBuffer class is mutable sequence of characters. StringBuffer can be used to modify the content of a String with ease. It provides many utility functions to manipulate a string. A StringBuffer opearations are syncronized in nature and it is recommanded to be used in multi-threading environment. In case, syncronization is not needed, we can go for an alternate API StringBuffer.

StringBuffer Class Declaration

Following is the declaration for java.lang.StringBuffer class −

public final class StringBuffer
   extends Object
      implements Serializable, CharSequence

StringBuffer Class Constructors

Following is the list of constructors of the StringBuffer class.

Sr.No. Constructor & Description
1

StringBuffer()

This constructs a string builder with no characters in it and an initial capacity of 16 characters.

2

StringBuffer(CharSequence seq)

This constructs a string builder that contains the same characters as the specified CharSequence.

3

StringBuffer(int capacity)

This constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

4

StringBuffer(String str)

This constructs a string builder initialized to the contents of the specified string.

StringBuffer Class methods

Following is the list of methods of the StringBuffer class. Each method is having multiple examples to demonstrate the functionality of the method.

Sr.No. Method & Description
1 StringBuffer append()

This method appends the given string argument to the sequence.

2 StringBuffer appendCodePoint(int codePoint)

This method appends the string representation of the codePoint argument to this sequence.

3 int capacity()

This method returns the current capacity.

4 char charAt(int index)

This method returns the char value in this sequence at the specified index.

5 int codePointAt(int index)

This method returns the character (Unicode code point) at the specified index.

6 int codePointBefore(int index)

This method returns the character (Unicode code point) before the specified index.

7 int codePointCount(int beginIndex, int endIndex)

This method returns the number of Unicode code points in the specified text range of this sequence.

8 StringBuffer delete(int start, int end)

This method removes the characters in a substring of this sequence.

9 StringBuffer deleteCharAt(int index)

This method removes the char at the specified position in this sequence.

10 void ensureCapacity(int minimumCapacity)

This method ensures that the capacity is at least equal to the specified minimum.

11 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Characters are copied from this sequence into the destination character array dst.

12 int indexOf(String str)

This method returns the index within this string of the first occurrence of the specified substring.

13 StringBuffer insert()

This method inserts the string representation of the given argument into this sequence.

14 int lastIndexOf(String str)

This method returns the index within this string of the rightmost occurrence of the specified substring.

15 int length()

This method returns the length (character count).

16 int offsetByCodePoints(int index, int codePointOffset)

This method returns the index within this sequence that is offset from the given index by codePointOffset code points.

17 StringBuffer replace(int start, int end, String str)

This method replaces the characters in a substring of this sequence with characters in the specified String.

18 StringBuffer reverse()

This method causes this character sequence to be replaced by the reverse of the sequence.

19 void setCharAt(int index, char ch)

Character at the specified index is set to ch.

20 void setLength(int newLength)

This method sets the length of the character sequence.

21 CharSequence subSequence(int start, int end)

This method returns a new character sequence that is a subsequence of this sequence.

22 String substring(int start)

This method returns a new String that contains a subsequence of characters currently contained in this character sequence.

23 String toString()

This method returns a string representing the data in this sequence.

24 void trimToSize()

This method attempts to reduce storage used for the character sequence.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.Object
  • java.lang.CharSequence

Example: Append a Boolean to the StringBuffer

The following example shows the usage of Java StringBuffer append(Boolean b) method. Here, we are instantiating a StringBuffer object "buff" with the string name "tuts". Then, we invoke the append() method using the "buff" object with a boolean argument "true". The return value will be the appended string name "tuts true". Similarly, we demonstrate another case using the string name "abcd" and a boolean argument "false".

package com.tutorialspoint;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer stringBuffer = new StringBuffer("tuts ");
      System.out.println("buffer = " + stringBuffer);

      // appends the boolean argument as string to the string stringBuffer
      stringBuffer.append(true);
      
      // print the string stringBuffer after appending
      System.out.println("After append = " + stringBuffer);

      stringBuffer = new StringBuffer("abcd ");
      System.out.println("stringBuffer = " + stringBuffer);
      
      // appends the boolean argument as string to the string stringBuffer    
      stringBuffer.append(false);
      
      // print the string stringBuffer after appending
      System.out.println("After append = " + stringBuffer);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

buffer = tuts
After append = tuts true
stringBuffer = abcd
After append = abcd false
Advertisements