how to append character at first position in stringbuilder java

how to append character at first position in stringbuilder java

To append a character at the beginning of a StringBuilder in Java, you can use the insert method. This method allows you to insert a character, or a sequence of characters, at a specific index in the StringBuilder.

Here's an example of how to use the insert method to append a character at the beginning of a StringBuilder:

StringBuilder sb = new StringBuilder("Hello");
sb.insert(0, 'H');
Source‮l.www:‬autturi.com

This will insert the character 'H' at the beginning of the StringBuilder, resulting in the string "HHello".

You can also use the insert method to insert a sequence of characters at the beginning of the StringBuilder. For example:

StringBuilder sb = new StringBuilder("Hello");
sb.insert(0, "Hello, ");

This will insert the string "Hello, " at the beginning of the StringBuilder, resulting in the string "Hello, Hello".

Created Time:2017-11-01 12:05:11  Author:lautturi