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:
import java.lang.*;
class GFG {
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
String newString = new String();
for ( int i = 0 ; i < originalString.length(); i++) {
newString += originalString.charAt(i);
if (i == index) {
newString += stringToBeInserted;
}
}
return newString;
}
public static void main(String[] args)
{
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);
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:
import java.lang.*;
class GFG {
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
String newString = originalString.substring( 0 , index + 1 )
+ stringToBeInserted
+ originalString.substring(index + 1 );
return newString;
}
public static void main(String[] args)
{
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);
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:
import java.lang.*;
class GFG {
public static String insertString(
String originalString,
String stringToBeInserted,
int index)
{
StringBuffer newString
= new StringBuffer(originalString);
newString.insert(index + 1 , stringToBeInserted);
return newString.toString();
}
public static void main(String[] args)
{
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);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Dec, 2018
Like Article
Save Article