Java StringBuffer insert() Method

Java StringBuffer insert() Method | The insert() method in the StringBuffer class is used to insert the given character or the string at the said index. There are many variations of the insert() method:-

  1. public synchronized StringBuffer insert(int offset, String str)
  2. public synchronized StringBuffer insert(int offset, Object obj)
  3. public synchronized StringBuffer insert(int offset, char[ ] str)
  4. public synchronized StringBuffer insert(int index, char[ ] str, int offset, int len)
  5. public StringBuffer insert(int dstOffset, CharSequence s)
  6. public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
  7. public StringBuffer insert(int offset, boolean b)
  8. public synchronized StringBuffer insert(int offset, char c)
  9. public StringBuffer insert(int offset, int i)
  10. public StringBuffer insert(int offset, long l)
  11. public StringBuffer insert(int offset, float f)
  12. public StringBuffer insert(int offset, double d)

All of the above insert() method throws StringIndexOutOfBoundsException. As we know StringBuffer class is synchronized and all its methods are also synchronized. But in the above list of insert() methods, some method doesn’t contain the synchronized keyword in the method declaration therefore don’t think that they are non-synchronized methods. Actually internally they call the insert(int, String) method which is synchronized. Hence synchronization is achieved via invocations of other StringBuffer methods. See more:- When To Use StringBuffer And StringBuilder In Java?

Example-1:
String: “Bok”
Insert ‘o’ at 2nd position
After insertion: “Book”

Example-2:
String: “Hello”
Insert “World” at 6th position
After insertion: “Hello World”

How To Insert String To StringBuffer Java

Let us see how to insert a string to StringBuffer in Java using the insert() method.

public class Main {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Learning Java Language");
      System.out.println("StringBuffer: " + sb);
      sb.insert(14, "Programming ");
      System.out.println("StringBuffer after appending: " + sb);
   }
}

Output:-

StringBuffer: Learning Java Language
StringBuffer after appending: Learning Java Programming Language

Now we are demonstrating the insert() method with a boolean value. The syntax for this variation is as follows:- public StringBuffer insert(int pos, boolean bool)
Parameters:
pos: the position from where to insert.
bool: the boolean value true or false.
Returns: the StringBuffer after insertion

public class Main {
   public static void main(String[] args) {
      String string = "Programmers typically define the boolean " 
                          + " terms to have values 1.";
      StringBuffer sb = new StringBuffer(string);
      System.out.println("StringBuffer = " + sb);
      sb.insert(41, true);
      System.out.println("StringBuffer after insertion = " + sb);
   }
}

Output:-

StringBuffer = Programmers typically define the boolean terms to have values 1.
StringBuffer after insertion = Programmers typically define the boolean true terms to have values 1.

Insert String To StringBuffer Java

Let us an example to insert the character to the StringBuffer. The syntax for the same is as follows:- public StringBuffer insert(int pos, char ch)
Parameters:
pos: the position from where to insert.
ch: the character needed to be inserted.
Returns: the StringBuffer after insertion.

public class Main {
   public static void main(String[] args) {
      String string = "insert ethod is used to insert the characters";
      StringBuffer sb = new StringBuffer(string);
      System.out.println("StringBuffer = " + sb);
      sb.insert(7, 'm');
      System.out.print("After insertion = " + sb);
   }
}

Output:-

StringBuffer = insert ethod is used to insert the characters
After insertion = insert method is used to insert the characters

Insert StringBuffer In Java

Now we are inserting the character array into the StringBuffer. The syntax to do this is as follows:- public StringBuffer insert(int pos, charArray[] charray)
Parameters:
pos: the position from where to insert.
charray: the character array needed to be inserted.
Returns: the StringBuffer after insertion.

public class Main {
   public static void main(String[] args) {
      StringBuffer string = new StringBuffer(" garden");
      System.out.println("StringBuffer = " + string);
      char array[] = { 'p', 'a', 'p', 'a', 'y', 'a' };
      string.insert(0, array);
      System.out.print("After insertion = " + string);
   }
}

Output:-

StringBuffer = garden
After insertion = papaya garden

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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