1. Overview
In this article, we will learn how to insert value or string at a particular index into StringBuilder in java.
StringBuilder is a mutable sequence of characters and it is recommended to use the non-synchronization applications.
StringBuilder class has a useful method insert() which takes the index and object as arguments.
When the data is added to the string builder at the given index then the remaining characters next to the given index are shifted to the right side. This is a costly operation in case of you have millions of characters in it.
2. Java StringBuilder insert() Syntax
StringBuilder.insert() method has many versions in overloaded style.
insert() method is added to add any type of data into the string builder.
StringBuilder insert(int offset, boolean b)
This inserts the string representation of the boolean argument into this sequence.
StringBuilder insert(int offset, char c)
This inserts the string representation of the char argument into this sequence.
StringBuilder insert(int offset, char[] str)
This inserts the string representation of the char array argument into this sequence.
StringBuilder insert(int index, char[] str, int offset, int len)
This inserts the string representation of a subarray of the str array argument into this sequence.
StringBuilder insert(int offset, double d)
This inserts the string representation of the double argument into this sequence.
StringBuilder insert(int offset, float f)
This inserts the string representation of the float argument into this sequence.
StringBuilder insert(int offset, int i)
This inserts the string representation of the second int argument into this sequence.
StringBuilder insert(int offset, long l)
This inserts the string representation of the long argument into this sequence.
StringBuilder insert(int dstOffset, CharSequence s)
This inserts the specified CharSequence into this sequence.
StringBuilder insert(int dstOffset, CharSequence s, int start, int end)
This inserts a subsequence of the specified CharSequence into this sequence.
StringBuilder insert(int offset, Object obj)
This inserts the string representation of the Object argument into this character sequence.
StringBuilder insert(int offset, String str)
This inserts the string into this character sequence.
3. Java StringBuilder Insert Examples
Look at the below examples for different data types at the given index.
We have covered the char, char array, double, string, double, long, float, boolean or any object in this example.
Example
Epackage com.javaprogramto.stringbuilder; import com.javaprogramto.models.Employee; public class StringBuilderInsertExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("first line end"); // adding boolean to the string builder sb.insert(10, true); sb.insert(10, " "); System.out.println(sb.toString()); // adding char to the string builder sb.insert(10, 'A'); sb.insert(10, " "); System.out.println(sb.toString()); // adding char array to the string builder char[] charArray = { 'B', 'C' }; sb.insert(10, charArray); sb.insert(10, " "); System.out.println(sb.toString()); // adding char array to the string builder char[] charArray2 = { 'F', 'D', 'G', 'H' }; sb.insert(10, charArray2, 1, 2); sb.insert(10, " "); System.out.println(sb.toString()); // adding double to the string builder double d = 1000; sb.insert(10, d); sb.insert(10, " "); System.out.println(sb.toString()); // adding int to the string builder int i = 1001; sb.insert(10, 1); sb.insert(10, " "); System.out.println(sb.toString()); // adding float to the string builder float f = 1000.009f; sb.insert(10, f); sb.insert(10, " "); System.out.println(sb.toString()); // adding long to the string builder long l = 1000; sb.insert(10, l); sb.insert(10, " "); System.out.println(sb.toString()); // adding StringBuffer to the string builder StringBuffer buffer = new StringBuffer("hello"); sb.insert(10, buffer); sb.insert(10, " "); System.out.println(sb.toString()); // adding StringBuffer to the string builder StringBuffer buffer2 = new StringBuffer("welcome "); sb.insert(10, buffer2, 3, 5); sb.insert(10, " "); System.out.println(sb.toString()); // adding StringBuffer to the string builder String str = "string"; sb.insert(10, str); sb.insert(10, " "); System.out.println(sb.toString()); // adding StringBuffer to the string builder Employee e = new Employee(100, "name", 50); sb.insert(10, e); sb.insert(10, " "); System.out.println(sb.toString()); } }
Output
first line true end first line A true end first line BC A true end first line DG BC A true end first line 1000.0 DG BC A true end first line 1 1000.0 DG BC A true end first line 1000.009 1 1000.0 DG BC A true end first line 1000 1000.009 1 1000.0 DG BC A true end first line hello 1000 1000.009 1 1000.0 DG BC A true end first line co hello 1000 1000.009 1 1000.0 DG BC A true end first line string co hello 1000 1000.009 1 1000.0 DG BC A true end first line Employee{id=100, fullName='name', age=50} string co hello 1000 1000.009 1 1000.0 DG BC A true end
we have added all types of data at index 10. For every insert at index 10 is shifted right side of values after index 10.
You can see the output when each time insert() method is called for the different data type.
4. Conclusion
In this article, we've seen how to insert the data at any index into the StringBuilder object or any primitive value or array.
In other words, any type of data can be added or inserted into the StringBuilder.
No comments:
Post a Comment
Please do not add any spam links in the comments section.