StringBuilder append to the start/front [3 ways]

In this post, I will be sharing how to append text to the start/front of the StringBuilder object. I will share examples of how to insert or add or prepend text at the beginning of the StringBuilder object. There are three ways to achieve our goal:

1. Using the insert() method

2. Using replace() method

3. Using the reverse() method

Read Also: Check if StringBuilder is empty in Java

Let's dive deep into the topic:

StringBuilder append to the start/front

1. Using insert() method of StringBuilder class


It is easy to append text to the start/front of the StringBuilder object using the insert() method. The insert() method of the StringBuilder class inserts the specified text at the specified index. The syntax of the insert() method is given below:

 public StringBuilder insert (int offset, String str)


Setting an offset to 0 means that you are appending to the front of the StringBuilder. You can find the example of the insert() method below:

 public class StringBuilderAppendToFront {
    public static void main(String args[]) {
      // Creating StringBuilder Object    
      StringBuilder sb = new StringBuilder();
      sb.append(" AAA ");
      sb.append(" BBB ");
      sb.append(" CCC ");
      /* Using insert() method to append " ZZZ "
      at the beginning of the StringBuilder at'
      the 0 index */
      sb.insert(0, " ZZZ ");
      System.out.println("StringBuilder append to front example:" + sb);
    }
}


Output:
StringBuilder append to front example: ZZZ AAA BBB CCC


2. Using replace() method of StringBuilder class


We can easily append text at the front of the StringBuilder using replace() method. The syntax of the replace() method is given below:

 public StringBuilder replace (int start, int end, String str)


You can find the example of replace() method below:

 public class StringBuilderAppendToFront2 {
    public static void main(String args[]) {
      // Making StringBuilder Object    
      StringBuilder sb2 = new StringBuilder();
      sb2.append("DDD");
      sb2.append("EEE");
      sb2.append("FFF");
      /* Using replace() method to append 
      "YYY" at the beginning of the StringBuilder
      at the 0 index */
      sb2.replace(0, 0, "YYY");
      System.out.println("StringBuilder append to front using replace() method example: \n" + sb2);
    }
}


Output:
StringBuilder append to front using replace() method example:
YYYDDDEEEFFF


3. Using reverse() method of StringBuilder class


You can add text at the start/front of the StringBuilder using the reverse() method. The syntax of the reverse() method is given below:
 public StringBuilder reverse()


You can find the example of adding text at the front/start of the StringBuilder using the reverse() method below:

 public class StringBuilderAppendToFront3 {
    public static void main(String args[]) {
      // Instantiating StringBuilder Object    
      StringBuilder sb3 = new StringBuilder();
      sb3.append("GGG");
      sb3.append("HHH");
      sb3.append("III");
      // String value to be added at the front
      String front = "XXX";
      // Reverse the StringBuilder
      sb3.reverse();
      // Append the front value to the StringBuilder object
      sb3.append(new StringBuilder(front).reverse());
      /* Using reverse() method to append 
      "XXX" at the beginning of the StringBuilder
      object */
      sb3.reverse();
      System.out.println("StringBuilder append to front using reverse() method example: \n" + sb3);
    }
}


Output:
StringBuilder append to front using reverse() method example:
XXXGGGHHHIII


That's all for today. Please mention in the comments if you have any questions related to how to append text to the start/front of the StringBuilder object.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry