Way2Java

Search, Append and Insert StringBuffer Methods

Very often used many StringBuffer Methods are discussed with Examples and Screenshots in Simple terms for a Beginner.

StringBuffer Methods for Searching characters

The indexOf(), lastIndexOf() and charAt() methods existing in String class also exist in StringBuffer class. They can be used with StringBuffer object. The meaning of the methods is just same of String class. Following program illustrates.

public class SearchingMethods
{
   public static void main(String args[])
   {
      StringBuffer buffer1 = new StringBuffer("pqrpqrpqr");
      System.out.println(buffer1.indexOf("qrp"));        // prints 1
      System.out.println(buffer1.indexOf("rpq", 3));     // prints 5
      System.out.println(buffer1.lastIndexOf("qr"));     // prints 7
      System.out.println(buffer1.charAt(5));             // prints r
   }
}

Output screen of SearchingMethods.java

Indexing methods are useful to know a specified string exists in a given string or not.

Note: indexOf() and lastIndexOf() methods that take character parameter do not exist in StringBufer. They exist in String class. So, do not use indexOf(char) with StringBuffer.

StringBuffer Methods for Appending to the Existing String

The existing contents of a StringBuffer can be appended with append() method. append() method is overloaded a number of times that can take any data type and object as parameter.

import java.util.*;          		// for Date class
public class AppendDemo
{
   public static void main(String args[])
   {
      String str1 = "Senior Trainer";
      boolean b1 = false;
      int x1 = 5 ;
      double d1 = 12.34;
      Date currentDay = new Date();
      String str2 = "Wishes" ;

      StringBuffer buffer1 = new StringBuffer();

      buffer1.append(str1);
      buffer1.append(b1);
      buffer1.append(x1); 
      buffer1.append(d1);   
      buffer1.append(' ');                       // appending one whitespace
      buffer1.append(currentDay) ;
      buffer1.append("   ");                    // appending empty string
      buffer1.append(str2);
      System.out.println(buffer1) ;
   }   
}
Output screen of AppendDemo.java

append() method is used to append different data types like boolean, int, double and objects like string and Date.

StringBuffer Methods for Inserting in the Middle

Altogether new data can be placed at any position in the existing string of StringBuffer. For this insert() method is overloaded a number of times that can accept data types, strings and arrays as parameters. Following program uses a few overloaded methods.

public class InsertDemo
{
   public static void main(String args[])
   {
      String s1 = "Best";
      String s2 = "Wishes";
      int appleCost = 12;
      char cyclone[] = { 'L', 'o', 'w', 'P', 'r', 'e', 's', 's', 'u', 'r', 'e' };

      StringBuffer buffer1 = new StringBuffer("Senior Trainer");
      buffer1.insert(buffer1.length(), s1);     // to insert at the end
      System.out.println(buffer1);         	// Senior Trainer Best

      buffer1.insert(0, s2);         	// to insert at the beginning
      System.out.println(buffer1);         	// WishesSenior TrainerBest

      StringBuffer buffer2 = new StringBuffer();  
      buffer2.insert(0, cyclone);         	// to insert array
      buffer2.insert(3, appleCost);         	// to place in middle
      System.out.println(buffer2);         	// Low12Pressure
  }
}
Output screen of InsertDemo.java

buffer1.insert(buffer1.length(), s1);

The above statement inserts the string s1 at the end of the StringBuffer, buffer1. buffer1.length() gives the position of insertion of string s1.

buffer1.insert(0, s2);

The above statement inserts the string s2 at the beginning (0 position) of the StringBuffer, buffer1.

buffer2.insert(3, appleCost);

appleCost, 12, is inserted at the 3rd position in StringBuffer, buffer2.