The insert() method of Java StringBuilder class is used to insert the given element at the specified position in this character sequence.
The Syntax of insert() method is:
//insert string "hello" at the position (index) 1 sb.insert(1, "hello"); //insert integer 100 at the index 2 sb.insert(2, 100); //insert boolean 'true' at the end of StringBuilder instance sb.insert(sb.length(), true);
Here, sb
is an object of StringBuilder class.
insert() Description
public StringBuilder insert(int offset, String str): It inserts the string str at the specified index offset
. Similarly, there are multiple variations of this insert method that allow insertion of various data types into a StringBuilder instance. These variations are:
public StringBuilder insert(int offset, int i) public StringBuilder insert(int offset, long l) public StringBuilder insert(int offset, double d) public StringBuilder insert(int offset, float f) public StringBuilder insert(int offset, char ch) public StringBuilder insert(int offset, boolean b) public StringBuilder insert(int offset, char[] str) public StringBuilder insert(int index, char[] str, int offset, int length) public StringBuilder insert(int offset, Object obj) public StringBuilder insert(int offset, String str) public StringBuilder insert(int dstOffset, CharSequence cs) public StringBuilder insert(int dstOffset, CharSequence cs, int start, int end)
This method throws IndexOutOfBoundsException
and StringOutOfBoundsException
, if specified offsets are invalid.
insert() Parameters
The insert()
method Parameters are:
- offset: It represents the index where the specified data needs to be inserted.
- start: It represents the start of the substring that needs to be inserted. (See example below)
- end: It represents the end of the substring that needs to be inserted.
- i, l, d, f, ch, b: Represents the values of data types that needs to inserted using insert() method.
- str: String that is inserted in the given StringBuilder character sequence.
insert() Return Value
- It returns a new StringBuilder object after insertion.
Example 1: Inserting int, float, double, long, char and boolean
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Text"); sb.insert(sb.length(), 1);//inserting int System.out.println(sb); StringBuilder sb2 = new StringBuilder("Text"); sb2.insert(0, true); //inserting boolean at start System.out.println(sb2); StringBuilder sb3 = new StringBuilder("Text"); sb3.insert(sb3.length(), 2.5f); //inserting float at end System.out.println(sb3); StringBuilder sb4 = new StringBuilder("Text"); sb4.insert(sb4.length(), 1000.555); //inserting double System.out.println(sb4); StringBuilder sb5 = new StringBuilder("Text"); sb5.insert(0, 50L); //inserting long System.out.println(sb5); StringBuilder sb6 = new StringBuilder("Text"); sb6.insert(sb6.length(), 'Z'); //inserting char System.out.println(sb6); } }
Output:
Example 2: StringBuilder insert(int offset, char[] str)
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("BeginnersBook"); System.out.println("String :"+sb); char[] chArray = {'.','c','o','m'}; // inserting char array at the end of string sb.insert(sb.length(),chArray); System.out.println("String after insertion: " + sb); } }
Output:
Example 3: StringBuilder insert(int dstOffset, CharSequence cs, int start, int end)
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Beginners.com"); System.out.println("Given String :"+sb); CharSequence cs = "MyBook12"; //inserting only a part of char sequence //start and end specifies the part of char sequence sb.insert(9,cs,2,6); System.out.println("String after insertion: " + sb); } }
Output:
Example 4: StringBuilder insert(int offset, String str)
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Welcome BeginnersBook.com"); System.out.println("Given String :"+sb); //insert string "to " at index 8 sb.insert(8, "to "); System.out.println("String after insertion: " + sb); } }
Output:
Example 5: StringBuilder insert(int offset, Object obj)
public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Text"); System.out.println("Given String :"+sb); Object obj = "MyObject";//string object Object obj2 = 100L; //long object Object obj3 = 102.5f; //float object //insert All three objects at the end of given string sb.insert(sb.length(), obj); sb.insert(sb.length(), obj2); sb.insert(sb.length(), obj3); System.out.println("String after insertion: " + sb); } }
Output: