How to insert a string in beginning of another string in java?



Let's learn how to add a string at the beginning of another string in Java. We can do this in the following ways:

Using the toCharArray()

toCharArray() method of the String class that helps us to convert a string into a character array. We will use this method to insert a string at the beginning of another string. Following are the steps to do so:

  • Get both strings, suppose we have a string str1 and the string to be added at the beginning of str1 is str2.
  • Create a character array with the sum of the lengths of the two Strings as its length.
  • Starting from the 0th position, fill each element in the array with the characters of str2.
  • Now, from the (length of str2) position to the end of the array, fill the character from the 1st array.

Example

Following is the Java program to insert a string at the beginning of another string using the toCharArray() method:

public class InsertString {
   public static void main(String args[]) {
      String str1 = "Aishwarya";
      String str2 = "Hello, ";
      char[] ch = new char[str1.length() + str2.length()];
      for(int i = 0; i < str2.length(); i++) {
         ch[i] = str2.charAt(i);
      }
      for(int i = str2.length(); i < ch.length; i++) {
         ch[i] = str1.charAt(i - str2.length());
      }
      System.out.println("String after inserting: " + new String(ch));
   }
}

Output

When the above code is executed, it produces the following output:

String after inserting: Hello, Aishwarya

Using the String concatenation operator

The String concatenation operator is a binary operator that is used for concatenating two strings. It is represented by the + symbol.

Follow the steps below:

  • Get the strings, str1 and str2.
  • Use the + operator to concatenate str2 and str1.

Example

Following is the Java program to insert a string at the beginning of another string using the String concatenation operator:

public class InsertString {
   public static void main(String args[]) {
      String str1 = "Aishwarya";
      String str2 = "Hello, ";
      String str3 = str2 + str1;
      System.out.println("String after inserting: " + str3);
   }
}

Output

When the above code is executed, it produces the following output:

String after inserting: Hello, Aishwarya

Using the StringBuilder class

The StringBuilder class provides a method called insert() which is used for inserting a string at a specific position in another string.

Follow the steps below:

  • Get the strings, str1 and str2.
  • Instantiate the StringBuilder class by passing str1 as a parameter.
  • Invoke the insert() method of the StringBuilder class and pass 0 as the first parameter and str2 as the second parameter.
  • Finally, convert the StringBuilder object to string using the toString() method.

Example

Following is the Java program to insert a string in the beginning of another string using the StringBuilder class:

public class InsertString {
   public static void main(String args[]) {
      String str1 = "Aishwarya";
      String str2 = "Hello, ";
      StringBuilder sb = new StringBuilder(str1);
      sb.insert(0, str2);
      System.out.println("String after inserting: " + sb.toString());
   }
}

Output

When the above code is executed, it produces the following output:

String after inserting: Hello, Aishwarya

Using the StringBuffer class

Java provides the StringBuffer class as a replacement for Strings in places where there is a necessity to make a lot of modifications to Strings of characters. You can modify/manipulate the contents of a StringBuffer over and over again without leaving behind a lot of new unused objects.

The append() method of this class accepts a String value as a parameter and adds it to the current StringBuffer object.

The toString() method of this class returns the contents of the current StringBuffer object as a String.

Follow the steps below in order to complete the task:

  • Get both strings, suppose we have a string str1 and the string to be added at the beginning of str1 is str2.
  • Create an empty StringBuffer object.
  • Initially, append str2 to the above created StringBuffer object, using the append() method, Then, append str1.
  • Finally, convert the StringBuffer String using the toString() method

Example

Following is the Java program to insert a string at the beginning of another string using the StringBuffer class:

public class InsertString {
   public static void main(String args[]) {
      String str1 = "Aishwarya";
      String str2 = "Hello, ";
      StringBuffer sb = new StringBuffer();
      sb.append(str2);
      sb.append(str1);
      System.out.println("String after inserting: " + sb.toString());
   }
}
Updated on: 2025-09-01T14:02:49+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements