StringBuffer Methods in Java

The String class maintain many efficiency for processing String. However , once a String object is build,its contents will remain same.String object are constant string and StringBuffer object are modified strings. Java can perform certain development involving String because it knows that these object will not change.

Class StringBuffer provides three constructors that alows StringBuffer to be initialized with no characters and an initial quantity of 16 characters ; with no characters and an initial quantity described in the integer argument ; or with a copy of the character of the String argument and an initial quantity which is the number of character in the String argument plus 16.

public class StringBufferConstructer {
    public static void main (String args[]){
        StringBuffer buf1,buf2,buf3;
        buf1 = new StringBuffer();
        buf2 = new StringBuffer( 10 );
        buf3 = new StringBuffer("devstudioonline");
        String Output = 
        "buf1 = " + "\"" + buf1.toString() + "\""+
        "\nbuf2 = " + "\"" + buf2.toString() +"\"" + 
        "\nbuf3 = " + "\"" + buf3.toString() +"\"" ;
         System.out.println(Output);
    }
}

Output:

buf1 = ""
buf2 = ""
buf3 = "devstudioonline"

 

StringBuffer  length :-  Class StringBuffer provides the length methods to return the number of character currently stored in a StringBuffer .

StringBuffer capacity :- Class StringBuffer provides the capacity returns the number of character that can be stored in a StringBuffer without allocating more memory.

StringBuffer setLength :- Method setLength  is provided to allow the programmer  to increase or decrease the length of a StringBuffer.

StringBuffer ensureCapacity :- Method ensureCapacity ensure that a StringBuffer has a minimum capacity.

public class StringBufferCapacitylength{
    public static void main (String args[]){
    StringBuffer buf = 
    new StringBuffer("Hello, How are you?");
    String Output = "buf = " + buf.toString()+
                  "\nlength = " +buf.length()+
                  "\ncapacity = " +buf.capacity();
                  buf.ensureCapacity (55);
                  Output +="\n\nNew capacity = " + buf.capacity();
                  buf.setLength(8);
                  Output +="\n\nNew length = " + buf.length() +
                  "\nbuf = " + buf.toString();
                  System.out.println(Output);
    }

}

Output:

buf = Hello, How are you?
length = 19
capacity = 35

New capacity = 72

New length = 8
buf = Hello, H

 

StringBuffer charAt :-The charAt returns the character at the specified index.

StringBuffer setCharAt :- Method setCharAt sets the character at the specified position. 

StringBuffer getChars :- Method getChar returns a character array containing a copy of the character in the StringBuffer.

StringBuffer reverse :- It reverse the contents of the StringBuffer

//The charAt, setCharAt, getChars, and reverse methods of class StringBuffer

public class StringBufferChars {
   public static void main(String args[]) {
      StringBuffer buffer = new StringBuffer("hello Students this is example");
      String output = "buffer = " + buffer.toString() +
      "\nCharacter at 1: " + buffer.charAt(1) +
      "\nCharacter at 8: " + buffer.charAt(8);
      char charArray[] = new char[buffer.length()];
      buffer.getChars(0, buffer.length(), charArray, 0);
      output += "\n\nThe character are:";

      for (int i = 0; i < charArray.length; i++)
          output += charArray[i];
      buffer.setCharAt(2, 'H');
      buffer.setCharAt(6, 'T');
      output += "\n\nbuffer = " + buffer.toString();

      buffer.reverse();
      output += "\n\nbuffer = " + buffer.toString();

      System.out.println(output);
   }
}

Output:

buffer = hello Students this is example
Character at 1: e
Character at 8: u

The character are:hello Students this is example

buffer = heHlo Ttudents this is example

buffer = elpmaxe si siht stnedutT olHeh

 StringBuffer append Methods

The append methods are used by the Java compiler to implement the + and += operators for concatenating Strings.It allow the 10 overloaded append methods to allow various data type values to be added to the end of the StringBuffer.

//The  append methods of the StringBuffer class.

public class StringBufferAppend {
 public static void main(String args[]) {
  Object o = "hello everyone";
  String s = "good morning";
  char charArray[] = {
   'a',
   'b',
   'c',
   'd',
   'e',
   'f'
  };
  boolean b = true;
  char c = 'k';
  int i = 3;
  long l = 100000;
  float f = 12.50 f;
  double d = 77.353;
  StringBuffer buf = new StringBuffer();
  buf.append(l);
  buf.append(" ");
  buf.append(s);
  buf.append(" ");
  buf.append(charArray);
  buf.append(" ");
  buf.append(charArray, 0, 3);
  buf.append(" ");
  buf.append(b);
  buf.append(" ");
  buf.append(c);
  buf.append(" ");
  buf.append(i);
  buf.append(" ");
  buf.append(f);
  buf.append(" ");
  buf.append(d);
  System.out.println("String buffer = " + buf.toString());
 }
}

Output:
String buffer = 100000 good morning abcdef abc true k 3 12.5 77.353

 

StringBuffer Insertion and Delete Methods

Class StringBuffer provides 9 overloaded insert  method to insert  primitive data type , character array, String and object values at any position in a StringBuffer.

Class String provides methods delete and deleteCharAt for deleting characters at any position in a StringBuffer. 

//The  Insert and delete methods of the StringBuffer class.

public class StringBufferInsert {
 public static void main(String args[]) {
  Object o = "hello Everyone";
  String s = "Good Morning";
  char charArray[] = {
   'a',
   'b',
   'c',
   'd',
   'e',
   'f'
  };
  boolean b = true;
  char c = 'k';
  int i = 3;
  long l = 100000;
  float f = 12.50 f;
  double d = 77.353;
  StringBuffer buf = new StringBuffer();

  buf.insert(0, o);
  buf.insert(0, " ");
  buf.insert(0, s);
  buf.insert(0, " ");
  buf.insert(0, charArray);
  buf.insert(0, " ");
  buf.insert(0, b);
  buf.insert(0, " ");
  buf.insert(0, c);
  buf.insert(0, " ");
  buf.insert(0, i);
  buf.insert(0, " ");
  buf.insert(0, l);
  buf.insert(0, " ");
  buf.insert(0, f);
  buf.insert(0, " ");
  buf.insert(0, d);

  String output = "buffer after inserts is:\n" + buf.toString();
  buf.deleteCharAt(10);
  buf.delete(2, 6);
  output += "\n\nbuffer after deletes is:\n" + buf.toString();
  System.out.println(output);
 }
}

Output:
buffer after inserts is:
77.353 12.5 100000 3 k true abcdef Good Morning hello Everyone

buffer after deletes is:
77 12. 100000 3 k true abcdef Good Morning hello Everyone


Keywords: