The Wayback Machine - https://web.archive.org/web/20240726042512/https://www.geeksforgeeks.org/stringbuffer-insert-java/
Open In App

StringBuffer insert() in Java

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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());
    }
}


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());
    }
}


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());
    }
}


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());
   }      
}


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());
   }      
}


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());
   }      
}


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());
   }      
}


Output:

string = geeks for geeks
After insertion = geeks fo10r geeks


Similar Reads

equals() on String and StringBuffer objects in Java
Consider the following codes in java: Java Code // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java Code // This program prints true class GFG { public
2 min read
StringBuffer deleteCharAt() Method in Java with Examples
The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of integer type which refers to the index of the char
2 min read
StringBuffer appendCodePoint() Method in Java with Examples
appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an integer value been assigned. --> appendCodePoint(
3 min read
StringBuffer delete() Method in Java with Examples
The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_point, int end_point) Parameters : The method acce
3 min read
StringBuffer replace() Method in Java with Examples
The StringBuffer.replace() is the inbuilt method which is used to replace the characters in a substring of this sequence with the characters in the specified String. Here simply the characters in the substring are removed and other char is inserted at the start.Syntax : public StringBuffer replace(int first, int last, String st) Parameters : The me
2 min read
StringBuffer reverse() Method in Java with Examples
The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the StringBuffer after reversing the characters. Examples
2 min read
StringBuffer append() Method in Java with Examples
Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boolean a) is an inbuilt method in Java which is used t
13 min read
StringBuffer setLength() in Java with Examples
The setLength(int newLength) method of StringBuffer class is the inbuilt method used to set the length of the character sequence equal to newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength. If the newLength passed as argument is greater than or equal to the old length, null charact
2 min read
StringBuffer subSequence() in Java with Examples
The subSequence(int start, int end) method of StringBuffer class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of the returned subsequence is end-start. So if start
3 min read
StringBuffer codePointCount() method in Java with Examples
The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of the text range and endIndex is index after the la
3 min read
three90RightbarBannerImg