Java StringBuilder.insert() – Examples

In this tutorial, we will learn about the Java StringBuilder.insert() function, and learn how to use this function to insert a value/object in StringBuilder sequence, with the help of examples.

insert(int offset, boolean b)

StringBuilder.insert() inserts the string representation of the boolean argument into this StringBuilder sequence.

ADVERTISEMENT

Syntax

The syntax of insert() function is

insert(int offset, boolean b)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
bThis boolean value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 1 – insert(offset, b)

In this example, we will initialize a StringBuilder, and insert a boolean true value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        boolean b = true;
        stringBuilder.insert(offset, b);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdetruefghijk

insert(int offset, char c)

StringBuilder.insert() inserts the string representation of the char argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, char c)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
cThis char value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 2 – insert(offset, c)

In this example, we will initialize a StringBuilder, and insert a char 'M' value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        char c = 'M';
        stringBuilder.insert(offset, c);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeMfghijk

insert(int offset, char[] str)

StringBuilder.insert() inserts the string representation of the char array argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, char[] str)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
strThis char array will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 3 – insert(offset, str)

In this example, we will initialize a StringBuilder, and insert a char array str at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        char[] str = {'X', 'Y', 'Z'};
        stringBuilder.insert(offset, str);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeXYZfghijk

insert(int index, char[] str, int offset, int len)

StringBuilder.insert() inserts the string representation of a subarray of the str array argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int index, char[] str, int offset, int len)

where

ParameterDescription
indexThe position at which insert operation happens in StringBuilder sequence.
strThis char array value will be inserted at given index.
offsetOffset of subarray in str.
lenLength of subarray in str.

Returns

The function returns reference to this StringBuilder object.

Example 4 – insert(index,str, offset, len)

In this example, we will initialize a StringBuilder, and insert a boolean true value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int index = 5;
        int offset = 2;
        int len = 5;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
        stringBuilder.insert(index, str, offset, len);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeCDEFGfghijk

insert(int offset, double d)

StringBuilder.insert() inserts the string representation of the double argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, double d)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
dThis double value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 5 – insert(offset, d)

In this example, we will initialize a StringBuilder, and insert a double value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        double d = 3.14526985;
        stringBuilder.insert(offset, d);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcde3.14526985fghijk

insert(int offset, float f)

StringBuilder.insert() inserts the string representation of the float argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, float f)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
fThis float value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 6 – insert(offset, f)

In this example, we will initialize a StringBuilder, and insert a float value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        float f = 3.14f;
        stringBuilder.insert(offset, f);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcde3.14fghijk

insert(int offset, int i)

StringBuilder.insert() inserts the string representation of the second int argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, int i)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
iThis integer value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 7 – insert(offset, i)

In this example, we will initialize a StringBuilder, and insert a integer value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        int i = 44;
        stringBuilder.insert(offset, i);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcde44fghijk

insert(int offset, long l)

StringBuilder.insert() inserts the string representation of the long argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, long l)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
lThis long value will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 8 – insert(offset, l)

In this example, we will initialize a StringBuilder, and insert a long value at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        long l = 444852489;
        stringBuilder.insert(offset, l);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcde444852489fghijk

insert(int dstOffset, CharSequence s)

StringBuilder.insert() inserts the specified CharSequence into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int dstOffset, CharSequence s)

where

ParameterDescription
dstOffsetThe index at which insert operation happens in StringBuilder sequence.
sThis CharSequence will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 9 – insert(dstOffset, s)

In this example, we will initialize a StringBuilder, and insert a char sequence XYZ at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        CharSequence s = "XYZ";
        stringBuilder.insert(offset, s);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeXYZfghijk

insert(int dstOffset, CharSequence s, int start, int end)

StringBuilder.insert() inserts a subsequence of the specified CharSequence into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int dstOffset, CharSequence s, int start, int end)

where

ParameterDescription
dstOffsetThe index at which insert operation happens in StringBuilder sequence.
sThis CharSequence value will be inserted at given offset.
startThe index at which subsequence starts in CharSequence s.
endThe index at which subsequence ends in CharSequence s.

Returns

The function returns reference to this StringBuilder object.

Example 10 – insert(dstOffset, s, start, end)

In this example, we will initialize a StringBuilder, and insert a sub sequence from CharSequence s, at offset 5 of StringBuilder sequence. The subsequence in s begin at start and stop at end.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        int start = 2;
        int end = 5;
        CharSequence s = "XYZ123456";
        stringBuilder.insert(offset, s, start, end);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeZ12fghijk

insert(int offset, Object obj)

StringBuilder.insert() inserts the string representation of the Object argument into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, Object obj)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
objThis object’s string representation will be inserted at given offset.

Returns

The function returns reference to this StringBuilder object.

Example 11 – insert(offset, obj)

In this example, we will initialize a StringBuilder, and insert an Object String value at offset 5. We will define a class Person and use its object to insert in StringBuilder sequence.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        Person obj = new Person("XYZ", 22);
        stringBuilder.insert(offset, obj);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

class Person{
    public String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
     
    public String toString() {
        return '[' + name + '-' + age + ']';
    }
}

Output

Before insert : abcdefghijk
After insert  : abcde[XYZ-22]fghijk

insert(int offset, String str)

StringBuilder.insert() inserts the string into this StringBuilder sequence.

Syntax

The syntax of insert() function is

insert(int offset, String str)

where

ParameterDescription
offsetThe index at which insert operation happens in StringBuilder sequence.
strThe string we would like to insert in StringBuilder sequence.

Returns

The function returns reference to this StringBuilder object.

Example 12 – insert(offset, str)

In this example, we will initialize a StringBuilder, and insert a string value "XYZ" at offset 5.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijk");
        System.out.println("Before insert : " + stringBuilder.toString());
        
        int offset = 5;
        String str = "XYZ";
        stringBuilder.insert(offset, str);
        System.out.println("After insert  : " + stringBuilder.toString());
    }
}

Output

Before insert : abcdefghijk
After insert  : abcdeXYZfghijk

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.insert() function, and also learnt how to use this function with the help of examples.