Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I insert a character at a specific index in a string builder variable? Fo

ID: 3930805 • Letter: H

Question

How do I insert a character at a specific index in a string builder variable?
For example:
StringBuilder temp = new StringBuilder(how to);
I want to insert char 'j' at temp[3];
Temp would now = howj to. Thanks. How do I insert a character at a specific index in a string builder variable?
For example:
StringBuilder temp = new StringBuilder(how to);
I want to insert char 'j' at temp[3];
Temp would now = howj to. Thanks.
For example:
StringBuilder temp = new StringBuilder(how to);
I want to insert char 'j' at temp[3];
Temp would now = howj to. Thanks.

Explanation / Answer

public class StringBuilderDemo {

public static void main(String[] args) {

StringBuilder temp = new StringBuilder("how to");

// insert character value at position 3
temp.insert(3, 'j');

System.out.println(temp.toString());
}
}

/* sample output

howj to

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote