StringBuffer insert() in Java

insert(): This method is available in java.lang.StringBuffer class of Java.

Syntax:

StringBuffer java.lang.StringBuffer.insert(int offset, Object obj)

This method takes two arguments, one of type int and another of type Object (like boolean, int, char, etc). Inserts the string representation of the Object (like boolean, int, char, etc) argument into this character sequence.

Note: The offset argument must be greater than or equal to 0, and less than or equal to the length of this sequence.

Parameters: Two parameters are required for this method.

offset the offset.

obj: an Object.

Returns: a reference to this object.

Throws:

StringIndexOutOfBoundsException - if the offset is invalid.

Approach 1: When an offset value is in the range.

Java

public class InsertStringBuffer {
    public static void main(String[] args) {

        StringBuffer str = new StringBuffer("Hello World");

        // insert boolean
        int offset = 3;
        boolean b = true;
        str.insert(offset, b);
        System.out.println("After boolean insert = " + str);

        // insert char
        offset = 4;
        char c = 'a';
        str.insert(offset, c);
        System.out.println("After char insert = " + str);

        // insert char array
        offset = 5;
        char arr[] = { 'A''B''C' };
        str.insert(offset, arr);
        System.out.println("After char array insert = " + str);

        // insert CharSequence
        offset = 6;
        CharSequence s = "XYZ";
        str.insert(offset, s);
        System.out.println("After CharSequence insert = " + str);

        // insert double
        offset = 5;
        double d = 1888.9;
        str.insert(offset, d);
        System.out.println("After double insert = " + str);

        // insert float
        offset = 2;
        float f = 98.5f;
        str.insert(offset, f);
        System.out.println("After float insert = " + str);

        // insert int
        offset = 4;
        int i = -7;
        str.insert(offset, i);
        System.out.println("After int insert = " + str);

        // insert long
        offset = 5;
        long l = 477272;
        str.insert(offset, l);
        System.out.println("After long insert = " + str);

        // insert String
        offset = 4;
        String str1 = "WQA";
        str.insert(offset, str1);
        System.out.println("After String insert = " + str);

    }
}


Output:

After boolean insert = Heltruelo World After char insert = Heltaruelo World After char array insert = HeltaABCruelo World After CharSequence insert = HeltaAXYZBCruelo World After double insert = Helta1888.9AXYZBCruelo World After float insert = He98.5lta1888.9AXYZBCruelo World After int insert = He98-7.5lta1888.9AXYZBCruelo World After long insert = He98-4772727.5lta1888.9AXYZBCruelo World After String insert = He98WQA-4772727.5lta1888.9AXYZBCruelo World


Approach 2: When an offset value is out of range.

Java

public class InsertStringBuffer {
    public static void main(String[] args) {

        StringBuffer str = new StringBuffer("Hello World");

        int offset = 15;
        Object ob = 12;

        str.insert(offset, ob);
        System.out.println(str);

    }
}


Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 15, length 11 at java.base/java.lang.String.checkOffset(String.java:3704) at java.base/java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:1166) at java.base/java.lang.StringBuffer.insert(StringBuffer.java:541)


No comments:

Post a Comment