Java StringBuilder insert() Method Example

Java StringBuilder insert() method has 12 overloaded versions. Here are all of its forms:
  1. StringBuilder insert(int offset, boolean b) - Inserts the string representation of the boolean argument into this sequence.
  2. StringBuilder insert(int offset, char c) - Inserts the string representation of the char argument into this sequence.
  3. StringBuilder insert(int offset, char[] str) - Inserts the string representation of the char array argument into this sequence.
  4. StringBuilder insert(int index, char[] str, int offset, int len) - Inserts the string representation of a subarray of the str array argument into this sequence.
  5. StringBuilder insert(int dstOffset, CharSequence s) - Inserts the specified CharSequence into this sequence.
  6. StringBuilder insert(int dstOffset, CharSequence s, int start, int end) - Inserts a subsequence of the specified CharSequence into this sequence.
  7. StringBuilder insert(int offset, double d) - Inserts the string representation of the double argument into this sequence.
  8. StringBuilder insert(int offset, float f) - Inserts the string representation of the float argument into this sequence.
  9. StringBuilder insert(int offset, int i) - Inserts the string representation of the second int argument into this sequence.
  10. StringBuilder insert(int offset, long l) - Inserts the string representation of the long argument into this sequence.
  11. StringBuilder insert(int offset, Object obj) - Inserts the string representation of the Object argument into this character sequence.
  12. StringBuilder insert(int offset, String str) - Inserts the string into this character sequence.

Java StringBuilder insert() Method Example

Below simple program demonstrate the usage of all insert() overloaded methods.
public class InsertExample { public static void main(String[] args) { // 12 insert overloaded method StringBuilder builder = new StringBuilder("javaguides").insert(1, true); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, 'J'); System.out.println(builder.toString()); char[] chars = { 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r' }; builder = new StringBuilder("javaguides").insert(4, chars); System.out.println(builder.toString()); CharSequence charSequence = new StringBuilder("J2EE/"); builder = new StringBuilder("javaguides").insert(0, charSequence); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, 100.0d); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, 100.0f); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, 100); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, 100l); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, new Object()); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, "ultimate"); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, chars, 0, chars.length); System.out.println(builder.toString()); builder = new StringBuilder("javaguides").insert(0, charSequence, 0, charSequence.length()); System.out.println(builder.toString()); } }
Output:
jtrueavaguides Jjavaguides javadeveloperguides J2EE/javaguides 100.0javaguides 100.0javaguides 100javaguides 100javaguides java.lang.Object@15db9742javaguides ultimatejavaguides developerjavaguides J2EE/javaguides

Reference


Comments