Java StringBuffer insert() Method

The StringBuffer.insert() method in Java is used to insert the string representation of various data types into a StringBuffer object at a specified position. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. insert Method Syntax
  3. Examples
    • Inserting Boolean
    • Inserting Character
    • Inserting Character Array
    • Inserting Subarray of Character Array
    • Inserting Double
    • Inserting Float
    • Inserting Integer
    • Inserting Long
    • Inserting CharSequence
    • Inserting Subsequence of CharSequence
    • Inserting Object
    • Inserting String
  4. Conclusion

Introduction

The insert() method is a member of the StringBuffer class in Java. It allows you to insert various types of data into a StringBuffer at a specified position. This method is useful for dynamically building and modifying strings.

insert Method Syntax

The syntax for the insert method is as follows:

public synchronized StringBuffer insert(int offset, boolean b)
public synchronized StringBuffer insert(int offset, char c)
public synchronized StringBuffer insert(int offset, char[] str)
public synchronized StringBuffer insert(int index, char[] str, int offset, int len)
public synchronized StringBuffer insert(int offset, double d)
public synchronized StringBuffer insert(int offset, float f)
public synchronized StringBuffer insert(int offset, int i)
public synchronized StringBuffer insert(int offset, long l)
public synchronized StringBuffer insert(int dstOffset, CharSequence s)
public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
public synchronized StringBuffer insert(int offset, Object obj)
public synchronized StringBuffer insert(int offset, String str)

Parameters:

  • offset - the position at which to insert the data.
  • b - the boolean to be inserted.
  • c - the character to be inserted.
  • str - the character array to be inserted.
  • index - the position at which to insert the data.
  • len - the number of characters to insert.
  • d - the double to be inserted.
  • f - the float to be inserted.
  • i - the integer to be inserted.
  • l - the long to be inserted.
  • dstOffset - the position at which to insert the data.
  • s - the CharSequence to be inserted.
  • start - the start index of the subsequence to be inserted.
  • end - the end index of the subsequence to be inserted.
  • obj - the object whose string representation is to be inserted.
  • str - the string to be inserted.

Returns:

  • The StringBuffer object after the specified data has been inserted.

Examples

Inserting Boolean

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Insert a boolean at index 5
        sb.insert(5, true);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello true, World!

Inserting Character

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Insert a character at index 5
        sb.insert(5, 'X');

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

HelloX, World!

Inserting Character Array

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Insert a character array at index 7
        char[] chars = {'J', 'a', 'v', 'a'};
        sb.insert(7, chars);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, JavaWorld!

Inserting Subarray of Character Array

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Insert a subarray of character array at index 7
        char[] chars = {'J', 'a', 'v', 'a'};
        sb.insert(7, chars, 1, 3); // Inserts 'ava'

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, avaWorld!

Inserting Double

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("The value is: ");

        // Insert a double at index 14
        sb.insert(14, 3.14);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

The value is: 3.14

Inserting Float

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("The value is: ");

        // Insert a float at index 14
        sb.insert(14, 3.14f);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

The value is: 3.14

Inserting Integer

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("The number is: ");

        // Insert an integer at index 15
        sb.insert(15, 42);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

The number is: 42

Inserting Long

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("The number is: ");

        // Insert a long at index 15
        sb.insert(15, 123456789L);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

The number is: 123456789

Inserting CharSequence

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, !");

        // Insert a CharSequence at index 7
        CharSequence cs = "World";
        sb.insert(7, cs);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, World!

Inserting Subsequence of CharSequence

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, !");

        // Insert a subsequence of CharSequence at index 7
        CharSequence cs = "Wonderful World";
        sb.insert(7, cs, 10, 15); // Inserts 'World'

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, World!

Inserting Object

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("The object is: ");

        // Insert an object at index 16
        Object obj = 100;
        sb.insert(16, obj);

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

The object is: 100

Inserting String

public class StringBufferInsertExample {
    public static void main(String[] args) {
        // Create a StringBuffer object
        StringBuffer sb = new StringBuffer("Hello, !");

        // Insert a string at index 7
        sb.insert(7, "World");

        // Print the modified content of the StringBuffer
        System.out.println(sb.toString());
    }
}

Output:

Hello, World!

Conclusion

The StringBuffer.insert() method in Java provides a way to insert the string representation of various data types into a StringBuffer object at a specified position. By understanding how to use this method, you can dynamically build and modify strings efficiently. This method is particularly useful for applications that require dynamic string manipulation and modification.

Comments