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
- 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!");
sb.insert(5, ",");
System.out.println(sb.toString());
}
}
Output:
Hello, World!
Inserting a Character
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello World!");
sb.insert(5, ',');
System.out.println(sb.toString());
}
}
Output:
Hello, World!
Inserting Integer Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Value: ");
sb.insert(7, 100);
System.out.println(sb.toString());
}
}
Output:
Value: 100
Inserting Boolean Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Status: ");
sb.insert(8, true);
System.out.println(sb.toString());
}
}
Output:
Status: true
Inserting Double Values
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("PI: ");
sb.insert(4, 3.14159);
System.out.println(sb.toString());
}
}
Output:
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'};
sb.insert(6, charArray);
System.out.println(sb.toString());
}
}
Output:
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'};
sb.insert(6, charArray, 0, 3);
System.out.println(sb.toString());
}
}
Output:
Hello Wor!
Inserting a CharSequence
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello !");
CharSequence seq = "World";
sb.insert(6, seq);
System.out.println(sb.toString());
}
}
Output:
Hello World!
Inserting a Portion of a CharSequence
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello !");
CharSequence seq = "World";
sb.insert(6, seq, 1, 4);
System.out.println(sb.toString());
}
}
Output:
Hello orl!
Inserting an Object
public class StringBuilderInsertExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, ");
Object obj = "
World!";
sb.insert(7, obj);
System.out.println(sb.toString());
}
}
Output:
Hello, World!
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 various overloaded versions of this method, you can efficiently modify and build strings dynamically. Whether you need to insert strings, characters, numeric values, char arrays, or objects, the insert
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment