The StringBuilder.insert()
method in Java is used to insert various types of data into a StringBuilder
object at a specified position. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality, including the overloaded methods.
Table of Contents
- Introduction
insert
Method Syntax- Examples
- Inserting a String
- Inserting a Character
- Inserting Integer Values
- Inserting Boolean Values
- Inserting Double Values
- Inserting Char Arrays
- Inserting Substrings
- Inserting Objects
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.insert()
method is a member of the StringBuilder
class in Java. It allows you to insert various types of data at a specified position within the StringBuilder
object. This method is particularly useful when you need to modify a sequence of characters by adding new data at specific positions.
insert() Method Syntax
The StringBuilder
class provides several overloaded insert
methods:
insert(int offset, String str)
insert(int offset, char c)
insert(int offset, int i)
insert(int offset, long l)
insert(int offset, float f)
insert(int offset, double d)
insert(int offset, boolean b)
insert(int offset, char[] str)
insert(int offset, char[] str, int strOffset, int strLen)
insert(int offset, CharSequence s)
insert(int offset, CharSequence s, int start, int end)
insert(int offset, Object obj)
Method 1: insert(int offset, String str)
The syntax for inserting a string is as follows:
public StringBuilder insert(int offset, String str)
- offset: The position at which to insert the string.
- str: The string to be inserted.
Method 2: insert(int offset, char c)
The syntax for inserting a character is as follows:
public StringBuilder insert(int offset, char c)
- offset: The position at which to insert the character.
- c: The character to be inserted.
Method 3: insert(int offset, int i)
The syntax for inserting an integer is as follows:
public StringBuilder insert(int offset, int i)
- offset: The position at which to insert the integer.
- i: The integer to be inserted.
Method 4: insert(int offset, long l)
The syntax for inserting a long value is as follows:
public StringBuilder insert(int offset, long l)
- offset: The position at which to insert the long value.
- l: The long value to be inserted.
Method 5: insert(int offset, float f)
The syntax for inserting a float value is as follows:
public StringBuilder insert(int offset, float f)
- offset: The position at which to insert the float value.
- f: The float value to be inserted.
Method 6: insert(int offset, double d)
The syntax for inserting a double value is as follows:
public StringBuilder insert(int offset, double d)
- offset: The position at which to insert the double value.
- d: The double value to be inserted.
Method 7: insert(int offset, boolean b)
The syntax for inserting a boolean value is as follows:
public StringBuilder insert(int offset, boolean b)
- offset: The position at which to insert the boolean value.
- b: The boolean value to be inserted.
Method 8: insert(int offset, char[] str)
The syntax for inserting a char array is as follows:
public StringBuilder insert(int offset, char[] str)
- offset: The position at which to insert the char array.
- str: The char array to be inserted.
Method 9: insert(int offset, char[] str, int strOffset, int strLen)
The syntax for inserting a portion of a char array is as follows:
public StringBuilder insert(int offset, char[] str, int strOffset, int strLen)
- offset: The position at which to insert the char array portion.
- str: The char array to be inserted.
- strOffset: The starting position in the char array.
- strLen: The number of characters to insert.
Method 10: insert(int offset, CharSequence s)
The syntax for inserting a CharSequence is as follows:
public StringBuilder insert(int offset, CharSequence s)
- offset: The position at which to insert the CharSequence.
- s: The CharSequence to be inserted.
Method 11: insert(int offset, CharSequence s, int start, int end)
The syntax for inserting a portion of a CharSequence is as follows:
public StringBuilder insert(int offset, CharSequence s, int start, int end)
- offset: The position at which to insert the CharSequence portion.
- s: The CharSequence to be inserted.
- start: The starting position in the CharSequence.
- end: The ending position in the CharSequence.
Method 12: insert(int offset, Object obj)
The syntax for inserting an object is as follows:
public StringBuilder insert(int offset, Object obj)
- offset: The position at which to insert the object.
- obj: The object to be inserted.
Examples
Inserting a String
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World!");
// Insert a string at index 5
sb.insert(5, ", Beautiful");
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Hello, Beautiful World!
Inserting a Character
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World!");
// Insert a character at index 5
sb.insert(5, ',');
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Hello, World!
Inserting Integer Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Value: ");
// Insert an integer at index 7
sb.insert(7, 100);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Value: 100
Inserting Boolean Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Status: ");
// Insert a boolean at index 8
sb.insert(8, true);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Status: true
Inserting Double Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("PI: ");
// Insert a double value at index 4
sb.insert(4, 3.14159);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: PI: 3.14159
Inserting Char Arrays
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello !");
char[] charArray = {'W', 'o', 'r', 'l', 'd'};
// Insert a char array at index 6
sb.insert(6, charArray);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Hello World!
Inserting a Portion of a Char Array
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello !");
char[] charArray = {'W', 'o', 'r', 'l', 'd'};
// Insert a portion of a char array at index 6
sb.insert(6, charArray, 0, 3);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert
: Hello Wor!
Inserting Substrings
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello !");
String str = "Beautiful World";
// Insert a substring at index 6
sb.insert(6, str, 0, 9);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Hello Beautiful!
Inserting Objects
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World!");
Object obj = "Beautiful ";
// Insert an object at index 6
sb.insert(6, obj);
// Print the result
System.out.println("StringBuilder after insert: " + sb.toString());
}
}
Output:
StringBuilder after insert: Hello Beautiful World!
Real-World Use Case
Example: Building a URL with Query Parameters
In a real-world scenario, you might need to construct a URL dynamically with query parameters based on user input or certain conditions. Using StringBuilder
with the insert
method allows you to construct the URL efficiently.
Example Code
public class URLBuilder {
public static void main(String[] args) {
StringBuilder url = new StringBuilder("https://example.com/search?");
String query = "Java";
String sort = "relevance";
boolean includeImages = true;
// Insert query parameters dynamically
url.append("q=").append(query);
url.append("&sort=").append(sort);
if (includeImages) {
url.append("&images=").append(includeImages);
}
// Print the constructed URL
System.out.println("Constructed URL: " + url.toString());
}
}
Output:
Constructed URL: https://example.com/search?q=Java&sort=relevance&images=true
Conclusion
The StringBuilder.insert()
method in Java is used for inserting various types of data into a StringBuilder
object at specified positions. By understanding how to use the overloaded methods, you can efficiently modify and build strings dynamically. Whether you need to insert strings, characters, numeric values, char arrays, substrings, or objects, the insert
method provides a reliable solution for these tasks.