Given a String, the task is to insert another string in between the given String at a particular specified index in Java.
Examples:
Input: originalString = "GeeksGeeks",
stringToBeInserted = "For",
index = 4
Output: "GeeksForGeeks"
Input: originalString = "Computer Portal",
stringToBeInserted = "Science ",
index = 8
Output: "Computer Science Portal"
The various methods to do this are as follows:
- Without using any pre-defined method
Approach:
- Get the Strings and the index.
- Create a new String
- Traverse the string till the specified index and copy this into the new String.
- Copy the String to be inserted into this new String
- Copy the remaining characters of the first string into the new String
- Return/Print the new String
Below is the implementation of the above approach:
Program:
// Java program to insert a string into another string// without using any pre-defined methodimportjava.lang.*;classGFG {// Function to insert stringpublicstaticString insertString(String originalString,String stringToBeInserted,intindex){// Create a new stringString newString =newString();for(inti =0; i < originalString.length(); i++) {// Insert the original string character// into the new stringnewString += originalString.charAt(i);if(i == index) {// Insert the string to be inserted// into the new stringnewString += stringToBeInserted;}}// return the modified StringreturnnewString;}// Driver codepublicstaticvoidmain(String[] args){// Get the StringsString originalString ="GeeksGeeks";String stringToBeInserted ="For";intindex =4;System.out.println("Original String: "+ originalString);System.out.println("String to be inserted: "+ stringToBeInserted);System.out.println("String to be inserted at index: "+ index);// Insert the StringSystem.out.println("Modified String: "+ insertString(originalString,stringToBeInserted,index));}}chevron_rightfilter_noneOutput:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
- Using String.substring() method
Approach:
- Get the Strings and the index.
- Create a new String
- Insert the substring from 0 to the specified (index + 1) using substring(0, index+1) method. Then insert the string to be inserted into the string. Then insert the remaining part of the original string into the new string using substring(index+1) method.
- Return/Print the new String
Below is the implementation of the above approach:
Program:
// Java program to insert a string into another string// without using any pre-defined methodimportjava.lang.*;classGFG {// Function to insert stringpublicstaticString insertString(String originalString,String stringToBeInserted,intindex){// Create a new stringString newString = originalString.substring(0, index +1)+ stringToBeInserted+ originalString.substring(index +1);// return the modified StringreturnnewString;}// Driver codepublicstaticvoidmain(String[] args){// Get the StringsString originalString ="GeeksGeeks";String stringToBeInserted ="For";intindex =4;System.out.println("Original String: "+ originalString);System.out.println("String to be inserted: "+ stringToBeInserted);System.out.println("String to be inserted at index: "+ index);// Insert the StringSystem.out.println("Modified String: "+ insertString(originalString,stringToBeInserted,index));}}chevron_rightfilter_noneOutput:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
- Using StringBuffer.insert() method
Approach:
- Get the Strings and the index.
- Create a new StringBuffer
- Insert the stringToBeInserted into the original string using StringBuffer.insert() method.
- Return/Print the String from the StringBuffer using StringBuffer.toString() method.
Below is the implementation of the above approach:
Program:
// Java program to insert a string into another string// without using any pre-defined methodimportjava.lang.*;classGFG {// Function to insert stringpublicstaticString insertString(String originalString,String stringToBeInserted,intindex){// Create a new StringBufferStringBuffer newString=newStringBuffer(originalString);// Insert the strings to be inserted// using insert() methodnewString.insert(index +1, stringToBeInserted);// return the modified StringreturnnewString.toString();}// Driver codepublicstaticvoidmain(String[] args){// Get the StringsString originalString ="GeeksGeeks";String stringToBeInserted ="For";intindex =4;System.out.println("Original String: "+ originalString);System.out.println("String to be inserted: "+ stringToBeInserted);System.out.println("String to be inserted at index: "+ index);// Insert the StringSystem.out.println("Modified String: "+ insertString(originalString,stringToBeInserted,index));}}chevron_rightfilter_noneOutput:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all possible ways to convert one string into another string | Edit-Distance
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Java Program to open the command prompt and insert commands
- StringBuffer insert() in Java
- Sort an Array and Insert an Element Inside Array in Java
- How to insert Slide From Bottom animation in RecyclerView in Android
- Moving a file from one directory to another using Java
- How a thread can interrupt an another thread in Java?
- How to call private method from another class in Java with help of Reflection API?
- In Java, Can we call the main() method of a class from another class?
- Convert String into comma separated List in Java
- How to share a captured Image to another App in Android
- Java program to merge two files into a third file
- Java program to merge two files alternatively into third file
- Java program to merge two files alternatively into third file
- Java | Converting an Image into Grayscale using cvtColor()
- Merge arrays into a new object array in Java
- Split a list into two halves in Java
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- How to convert a Stream into a Map in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

