Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/08/2025 By admin

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.

String s1 = MakeEasy

String s2= Selenium

indexToInsertS2= 4

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

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

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

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.

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

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.

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

Uncategorized

Post navigation

Previous Post: Handling JQuery Dialog Box In Selenium Webdriver
Next Post: TestNG Tutorials 67 : Sharing Data Among Test Methods in TestNG Using ITestContext

Related Posts

August 27, 2017 – Make Selenium Easy Uncategorized
REST Assured Tutorial 2 – Setup a Basic REST Assured Maven Project in Eclipse IDE Uncategorized
image – Make Selenium Easy Uncategorized
#5. OAuth 2.0 Flow – What Is A Refresh Token? Uncategorized
how to install testng plugin in eclipse Uncategorized
TestNG Tutorials 20: Overriding Of Test Methods In TestNG Class Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark