StringBuffer insert() in Java
Last Updated : 05 Dec, 2024
The StringBuffer.insert() method in Java allows us to insert a string representation of a given data type at a specified position in a StringBuffer. This method is useful when we need to modify a string at specific positions without creating a new string each time by making it more efficient than concatenation.
Example: In the following example, we will insert a character into a StringBuffer at a specified position.
Java // Java Program to demonstrate StringBuffer // insert() method import java.io.*; class GFG { public static void main(String[] args) { StringBuffer s = new StringBuffer("geeks for geeks"); // Initial string System.out.println("String: " + s); // Insert 'E' at position 2 s.insert(2, 'E'); // Prints StringBuffer after insertion System.out.println("After insertion: " + s); } }
OutputString: geeks for geeks After insertion: geEeks for geeks
Explanation: In this example, we insert the character "E" at index 2 in the StringBuffer. The original string "geeks for geeks" becomes "geEeks for geeks" after the insertion.
Syntax of StringBuffer.insert() Method
str.insert(int position, data_type value);
Parameters:
- position: This is the index in string where we need to insert.
- value: This is the element to be inserted at the position the data type of value can vary.
Return Type: This method returns a reference to the StringBuffer object.
Exception: The position must be greater than or equal to 0, and less than or equal to the length of the string.
In this example, we insert a boolean value at a given position in the StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for boolean input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Insert boolean value at offset 8 sb.insert(8, true); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks fotruer geeks
In this example, we insert a character array at a given position in the StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for char array input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Character array to be inserted char[] ch = {'J', 'a', 'v', 'a'}; // Insert character array at offset 8 sb.insert(8, ch); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks foJavar geeks
In this example, we insert a float value at a given position in the StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for float input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Insert float value at offset 8 sb.insert(8, 41.35f); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks fo41.35r geeks
In this example, we insert a double value into a StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for double input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Insert double value at offset 8 sb.insert(8, 41.35d); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks fo41.35r geeks
In this example, we insert a long value into a StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for Long input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Insert long value at offset 8 sb.insert(8, 546986L); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks fo546986r geeks
In this example, we insert an integer into a StringBuffer.
Java // Java program to demonstrate StringBuffer // insert() method for Int input public class GFG { public static void main(String[] args) { StringBuffer sb = new StringBuffer("geeks for geeks"); System.out.println("String: " + sb); // Insert int value at offset 8 int x = 10; sb.insert(8, x); System.out.println("After insertion: " + sb); } }
OutputString: geeks for geeks After insertion: geeks fo10r geeks
Similar Reads
StringBuffer Class in Java The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c
11 min read
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
Java StringBuffer capacity() Method In Java, the capacity() method is used in the StringBuilder and StringBuffer classes to retrieve the current capacity of the object. The capacity is the amount of space that has been allocated to store the string that can grow dynamically as needed.Example 1: The below Java program demonstrates the
3 min read
Java StringBuilder Class In Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations.Declaration:StringBuilder sb = new
7 min read
Java StringBuffer deleteCharAt() Method In Java, the deleteCharAt() method of the StringBuffer class is used to delete the character at a specified index in the string. Example 1: The below Java program demonstrates the use of deleteCharAt() to remove a character at a specific index from the string.Java// Removing a character at a specifi
2 min read
String Class in Java A string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
7 min read