Search, Append and Insert StringBuffer Methods

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.

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.

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.

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. Tanuj Sareen

    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. Tanuj Sareen

        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. PALLAVAN JPR

        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. S. Nageswara Rao, Corporate Trainer

          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 Reply

Your email address will not be published. Required fields are marked *