Menu
Topics Index
...
`


Strings > StringBuffer >
Siva Nookala - 05 Apr 2016
This method can be used to insert characters at a given position in a StringBuffer. It is overloaded to accept all the vlaues of the primitive types, String Object, character array and for Object.

StringBuffer Methods:
MethodDescription
StringBuffer insert(int offset, boolean b)Inserts the string representation of the boolean argument into this sequence.
StringBuffer insert(int offset, char c)Inserts the string representation of the char argument into this sequence.
StringBuffer insert(int offset, char[] str)Inserts the string representation of the char array argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)Inserts the string representation of a subarray of the strarray argument into this sequence.
StringBuffer insert(int dstOffset, CharSequence s)Inserts the specified CharSequence into this sequence.
StringBuffer insert(int dstOffset, CharSequence s, int start, int end)Inserts a subsequence of the specified CharSequenceinto this sequence.
StringBuffer insert(int offset, double d)Inserts the string representation of the double argument into this sequence.
StringBuffer insert(int offset, float f)Inserts the string representation of the float argument into this sequence.
StringBuffer insert(int offset, int i)Inserts the string representation of the second int argument into this sequence.
StringBuffer insert(int offset, long l)Inserts the string representation of the long argument into this sequence.
StringBuffer insert(int offset, Object obj)Inserts the string representation of the Object argument into this character sequence.
StringBuffer insert(int offset, String str)Inserts the string into this character sequence.

StringBufferDemo5
class StringBufferDemo5
{
    public static void main(String args[])
    {
    
        StringBuffer sb = new StringBuffer("Merit Campus "); // LINE A
        sb.insert(sb.length(), "Your Online Java School. "); // LINE B
        System.out.println(sb);
    }
}
OUTPUT

Merit Campus Your Online Java School.

DESCRIPTION

Firstly, "Merit Campus " is taken into the StringBuffer. At the end of the StringBuffer, String "Your Online Java School. " is inserted. Finally the StringBuffer is printed.

THINGS TO TRY
  • Place the below code after LINE B in the above program and check the output.
    CharSequence ch = "Don't Just Learn Java, Do Java";
    sb.insert(sb.length(), ch);
    System.out.println(sb);
    Output for the above code :
    Merit Campus Your Online Java School. Don't Just Learn Java, Do Java
RESET_CODE
  • Place the below code after LINE B in the above program and check the output.
    sb.insert(sb.length(), " Do Java with ");
    sb.insert(sb.length(), 600);
    System.out.print(sb + "+ practise programs.");
    Output for the above code :
    Merit Campus Your Online Java School. Do Java with 600+ practise programs.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App