StringBuffer insert() in Java
The StringBuffer.insert() method inserts the string representation of given data type at given position in a StringBuffer.
Syntax:
str.insert(int position, char x); str.insert(int position, boolean x); str.insert(int position, char[] x); str.insert(int position, float x); str.insert(int position, double x); str.insert(int position, long x); str.insert(int position, int x); position is the index in string where we need to insert.
Return:
This method returns a reference to this object.
Exception:
The position argument must be greater than or equal to 0, and less than or equal to the length of this string.
Boolean Input
// Java program to demonstrate StringBuffer insert // for boolean input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert boolean value at offset 8 str.insert(8, true); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fotruer geeks
Character Input
// Java program to demonstrate StringBuffer insert // for char input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert boolean value at offset 8 str.insert(8, 'p'); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fopr geeks
Character Array Input
// Java program to demonstrate StringBuffer insert // for char array input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // character array to be inserted char cArr[] = { 'p', 'a', 'w', 'a', 'n' }; // insert character array at offset 9 str.insert(8, cArr); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fopawanr geeks
Float Input
// Java program to demonstrate StringBuffer insert // for float input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert float value at offset 3 str.insert(8, 41.35f); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fo41.35r geeks
Double Input
// Java program to demonstrate StringBuffer insert // for double input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert float value at offset 3 str.insert(8, 41.35d); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fo41.35r geeks
Long Input
// Java program to demonstrate StringBuffer insert // for Long input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert float value at offset 3 str.insert(8, 546986L); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fo546986r geeks
Int Input
// Java program to demonstrate StringBuffer insert // for Int input. import java.lang.*; public class GFG { public static void main(String[] args) { StringBuffer str = new StringBuffer("geeks for geeks"); System.out.println("string = " + str); // insert float value at offset 8 int x = 10; str.insert(8, x); // prints stringbuffer after insertion System.out.print("After insertion = "); System.out.println(str.toString()); } } |
chevron_right
filter_none
Output:
string = geeks for geeks After insertion = geeks fo10r geeks
Recommended Posts:
- StringBuffer class in Java
- String vs StringBuilder vs StringBuffer in Java
- StringBuffer subSequence() in Java with Examples
- StringBuffer setLength() in Java with Examples
- A Java Random and StringBuffer Puzzle
- StringBuffer codePointBefore() method in Java with Examples
- Sorting collection of String and StringBuffer in Java
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- equals() on String and StringBuffer objects in Java
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



