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
   }
}

StringBuffer Methods
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) ;
   }   
}
StringBuffer Methods
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
  }
}
StringBuffer Methods
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.

8 thoughts on “Search, Append and Insert StringBuffer Methods”

  1. Sir,
    as u hav mentioned in the note:

    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.

    then y ,how u used it in StringBuffer in the starting. ?

      1. sir,
        just before: ” Appending to the Existing String ”

        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.

    1. String str = “hello” is a shortcut for String s1 = new String(“hello”);. That is, if you write String str = “hello”, JVM converts it to String s1 = new String(“hello”); internally. No such shortcut is available to StringBuffer or any other Java class.

      1. Hello sir….
        when creating two string literals like, String s1=”hello” and string s2=”hello”, the JRE creates only one memory location as both values are same. As for your above reply, the JVM converts it internally like “String s1=new String(“hello”) ” and “String s2=new String(“hello”) “. so there should be two different memory locations. how it is possible. ?

        1. String s1 = “hello; creates String s1 = new String(“hello”), but when later String s2 = “hello”
          exists, String s2 = new String(“hello”) is not created.

          When String s3 = new String(“hello”) and String s4 = new String(“hello”) exists, two locations is created because you are asking JVM to give two different locations with “new” keyword.

Leave a Comment

Your email address will not be published.