Insert a String into another String in Java

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 methodÂÂimportjava.lang.*;ÂÂclassGFG {   Â// Function to insert string   ÂpublicstaticString insertString(       ÂString originalString,       ÂString stringToBeInserted,       Âintindex)   Â{       Â// Create a new string       ÂString newString =newString();       Âfor(inti =0; i < originalString.length(); i++) {           Â// Insert the original string character           Â// into the new string           ÂnewString += originalString.charAt(i);           Âif(i == index) {               Â// Insert the string to be inserted               Â// into the new string               ÂnewString += stringToBeInserted;           Â}       Â}       Â// return the modified String       ÂreturnnewString;   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Strings       ÂString 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 String       ÂSystem.out.println("Modified String: "                          Â+ insertString(originalString,                                         ÂstringToBeInserted,                                         Âindex));   Â}}Output: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 methodÂÂimportjava.lang.*;ÂÂclassGFG {   Â// Function to insert string   ÂpublicstaticString insertString(       ÂString originalString,       ÂString stringToBeInserted,       Âintindex)   Â{       Â// Create a new string       ÂString newString = originalString.substring(0, index +1)                          Â+ stringToBeInserted                          Â+ originalString.substring(index +1);       Â// return the modified String       ÂreturnnewString;   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Strings       ÂString 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 String       ÂSystem.out.println("Modified String: "                          Â+ insertString(originalString,                                         ÂstringToBeInserted,                                         Âindex));   Â}}Output: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 methodÂÂimportjava.lang.*;ÂÂclassGFG {   Â// Function to insert string   ÂpublicstaticString insertString(       ÂString originalString,       ÂString stringToBeInserted,       Âintindex)   Â{       Â// Create a new StringBuffer       ÂStringBuffer newString           Â=newStringBuffer(originalString);       Â// Insert the strings to be inserted       Â// using insert() method       ÂnewString.insert(index +1, stringToBeInserted);       Â// return the modified String       ÂreturnnewString.toString();   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Get the Strings       ÂString 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 String       ÂSystem.out.println("Modified String: "                          Â+ insertString(originalString,                                         ÂstringToBeInserted,                                         Âindex));   Â}}Output:Original String: GeeksGeeks String to be inserted: For String to be inserted at index: 4 Modified String: GeeksForGeeks
Input: originalString = "Computer Portal",
stringToBeInserted = "Science ",
index = 8
Output: "Computer Science Portal"



