How we can insert a string into another string in Java

In this tutorial, we will see a program to insert a string into another string in Java. Consider the user enters two strings and an index value, we need to insert the second string at the given index of the first string.

Let us see an example,

Insert a string into another string in Java

Let us see the possible ways to solve the problem,

By Traversing the String

In this method, we will traverse through the string until we encounter the index given by the user. Once we reach the index value add the second string elements to the new string and continue with the original string.
Let us see the step by step procedure for this method,

  1. Input the original string from the user.
  2. Input the string that needs to be inserted (second string).
  3. Get the index value at which the second string needs to be inserted.
  4. Traverse the original string and add it’s elements one by one to a new string.
    Use the charAt() method to access each character of the string.
  5. Add the second string to the new string when you reach the index value.
  6. Continue by adding the original string elements and print the new string.

Let us see a Java program based on the following steps,

import java.lang.*; 
import java.util.*;  
class StrInsert { 
  
  public static void main(String[] args) 
    { 
        Scanner input=new Scanner(System.in);
        
        // Input original string
        System.out.println("Enter the Original String: ");
        String firStr = input.nextLine();
        
        //Input the second string to be inserted
        System.out.println("Enter the String to be Inserted: ");
        String secStr = input.nextLine();
        
        //Input the index after which the string should be inserted
        System.out.println("Enter the Index: ");
        int index = input.nextInt();
  
        String newStr = new String(); 
  
        for (int i = 0; i < firStr.length(); i++) { 

            //Add character by character to new string 
            newStr += firStr.charAt(i); 
           
            if (i == index) { 
                //Insert the second string
                newStr += secStr; 
            } 
        } 
  
        // Output 
        System.out.println("New String After Insertion: "+ newStr);
 
    } 
}

Output

Enter the Original String:
CODEY
Enter the String to be Inserted:
SPEED
Enter the Index:
3
New String After Insertion: CODESPEEDY

Using substring() method

In this method, we will be using the substring() method in Java to insert a string. This method belongs to the String class in Java. The substring() method returns a part of the string without changing the original string.

Syntax

stringVariable.substring(startIndex, endIndex);

Let us see the step by step procedure for this method,

  1. Input the original string from the user.
  2. Input the string that needs to be inserted (second string).
  3. Get the index value at which the second string needs to be inserted.
  4. Using substring() add a substring of the original string to the new string.
    The substring must be until the index value.
  5. Add the second string and finally add the remaining elements of the original string.

Java program based on the substring() concept,

import java.lang.*; 
import java.util.*;  
class StrInsert { 
  
  public static void main(String[] args) 
    { 
        Scanner input=new Scanner(System.in);
        
        // Input original string
        System.out.println("Enter the Original String: ");
        String firStr = input.nextLine();
        
        //Input the second string to be inserted
        System.out.println("Enter the String to be Inserted: ");
        String secStr = input.nextLine();
        
        //Input the index after which the string should be inserted
        System.out.println("Enter the Index: ");
        int index = input.nextInt();
  
        //New string using substring() method
        String newStr = firStr.substring(0, index + 1) 
                        + secStr  + firStr.substring(index + 1); 
  
  
        // Output 
        System.out.println("New String After Insertion: "+ newStr);
 
    } 
} 

Output

Enter the Original String:
CODEY
Enter the String to be Inserted:
SPEED
Enter the Index:
3
New String After Insertion: CODESPEEDY

We can notice that both methods yield the same output. Hence we can conclude that in Java we can insert a string into another string using these two methods.

You can also learn,
Java program for inserting double quotes into a string

 

Leave a Reply

Your email address will not be published. Required fields are marked *