Frequently Asked Java Programs: 33 – Insert a String into Another String at given index

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to Insert a String into Another String at given index.

Problem Statement:-

String s1 = MakeEasy

String s2= Selenium

indexToInsertS2= 4

Insert s2 in to s1 at index 4th. So s1 should be “MakeSeleniumEasy”.

Solution:-

We will see three approaches to solve this because interviewer may ask you not to use readymade methods.

Approach 1:-

Using StringBuilder or StringBuffer class:

There is a method called insert() in both classes which you can use to insert a string at given index.

Java Program:-

package StringPrograms;

public class InsertStringIntoStringUsingStringBuilder {
	
	
	public static void main(String[] args) {
		
		StringBuilder s1= new StringBuilder("MakeEasy");
		StringBuilder s2= new StringBuilder("Selenium");
		int indexToInsert= 4; 
		
		System.out.println("String s1 before inserting s2 at index "+indexToInsert+ ": "+s1);
		
		// Inserting at given index using insert() method. 
		// This method will add s2 at 4th index and then remaining string of s1 will be appended.
		s1.insert(indexToInsert, s2);
		
		System.out.println("String s1 after inserting s2 at index "+indexToInsert+ ": "+s1);
		
	}

}

Output:-

String s1 before inserting s2 at index 4: MakeEasy
String s1 after inserting s2 at index 4: MakeSeleniumEasy

Approach 2:-

Using substring() method:-

We need to divide and concat strings. This approach will have two steps:-

  1. Divide string S1 in to two parts. First part will start from first index i.e zero till index to insert String s2 minus 1. Second part will be from index to insert String s2 till last.

E.g. “MakeEasy” will be divided into two parts:- (0,3) and (4,7) i.e. “Make” & “Easy”.

We will use substring() method for the same. Since substring() method excludes provided last index so no need to do minus 1.

2. Concat String s2 in between two parts:- part1+s2+part2.

Java Program:-

package StringPrograms;

public class InsertStringIntoStringUsingSubstringMethod {
	
	
	public static void main(String[] args) {
		
		String s1= "MakeEasy";
		String s2= "Selenium";
		int indexToInsert= 4; 
		
		System.out.println("String s1 before inserting s2 at index "+indexToInsert+ ": "+s1);
		
		/*
		 * Its simple dividing and concatenation of string. We will divide s1 in to two parts. 
		 * First part will be from starting index till just before index to start. Since substring() method
		 * excludes last index so no need to do minus 1. Second part will be from index to start till last. 
		 * Add s2 in between. 
		 */
		s1= s1.substring(0,indexToInsert)+s2+s1.substring(indexToInsert);
		
		
		System.out.println("String s1 after inserting s2 at index "+indexToInsert+ ": "+s1);
		
	}

}

Output:-

String s1 before inserting s2 at index 4: MakeEasy
String s1 after inserting s2 at index 4: MakeSeleniumEasy

Approach 3:

Using loop:-

We need to iterate s1 char by char by index. Concatenate char by char till iterating index is same as index to insert string s2 i.e. indexToInsertS2 . Once both are same, Concatenate whole s2 with concatenated string followed by current indexed char of s1. Continue the for loop to Concatenate remaining chars of String s1.

Java Program:-

package StringPrograms;

public class InsertStringIntoStringUsingSubstringMethod {
	
	
	public static void main(String[] args) {
		
		String s1= "MakeEasy";
		String s2= "Selenium";
		int indexToInsert= 4; 
		
		System.out.println("String s1 before inserting s2 at index "+indexToInsert+ ": "+s1);
		
		String resultantString = "";
		
		/*
		 * Iterate s1 by index and concatenate char by char. If index matches with index to insert s2, concatenate s2 followed by 
		 * indexed char of s1. 
		 */
		
		for(int i = 0 ; i< s1.length() ; i++)
		{
			if(i != indexToInsert)
				resultantString += s1.charAt(i);
			else
				resultantString += s2+s1.charAt(i) ;
		}
		
		s1= resultantString;
		
		
		System.out.println("String s1 after inserting s2 at index "+indexToInsert+ ": "+s1);
		
	}

}

Output:-

String s1 before inserting s2 at index 4: MakeEasy
String s1 after inserting s2 at index 4: MakeSeleniumEasy

#HappyCoding

Leave a Reply

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