Last Updated : 11 Jul, 2025
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:
// Java program to insert a string into another string
// without using any pre-defined method
import java.lang.*;
class GFG {
// Function to insert string
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
// Create a new string
String newString = new String();
for (int i = 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
return newString;
}
// Driver code
public static void main(String[] args)
{
// Get the Strings
String originalString = "GeeksGeeks";
String stringToBeInserted = "For";
int index = 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
// Java program to insert a string into another string
// without using any pre-defined method
import java.lang.*;
class GFG {
// Function to insert string
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
// Create a new string
String newString = originalString.substring(0, index + 1)
+ stringToBeInserted
+ originalString.substring(index + 1);
// return the modified String
return newString;
}
// Driver code
public static void main(String[] args)
{
// Get the Strings
String originalString = "GeeksGeeks";
String stringToBeInserted = "For";
int index = 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
// Java program to insert a string into another string
// without using any pre-defined method
import java.lang.*;
class GFG {
// Function to insert string
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
// Create a new StringBuffer
StringBuffer newString
= new StringBuffer(originalString);
// Insert the strings to be inserted
// using insert() method
newString.insert(index + 1, stringToBeInserted);
// return the modified String
return newString.toString();
}
// Driver code
public static void main(String[] args)
{
// Get the Strings
String originalString = "GeeksGeeks";
String stringToBeInserted = "For";
int index = 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4