Insert a String into another String in Java
The task of the provided string is to insert a new string at a specific index in Java between the given string.
Example 1:
Input:
StringOriginal = "Hello World",
InsertedString = "Welcome To ",
Atindex = 5
Output:
The string after the insertion of another string is "Hello, Welcome To World."
Example 2:
Input:
StringOriginal = "JAVALanguage",
InsertedString = " is a programming ",
Atindex = 3
Output:
The string after insertion of another string is JAVA is a programming Language
There are various methods to implement this task, which are specified below.
Approach: Without using any pre-defined method
By applying this technique, we are going to iterate through the string until we reach the user-specified index. Add the elements of the second string to the new string once we reach the index value, then proceed with the original string.
Algorithm:
Step 1: Determine the index and the strings.
Step 2: Make an original String
Step 3: Copy the text at the designated index after traversing the string and copying it into the new string.
Step 4: Make a copy of the String that will be entered into this new String.
Step 5: Move the first string's remaining characters to the new string.
Step 6: Print or Return the New String.
Implementation:
File Name: StringInsertionPredefined.java
import java.io.*;
import java.util.*;
import java.lang.*;
public class StringInsertionPredifined
{
// The string insertion function
public static String insertString( String Stringoriginal, String InsertedString, int Atindex)
{
// Construct a new string
String newStr = new String();
for (int i = 0; i < Stringoriginal.length(); i++)
{
// Enter the first character of the original string
// into the new string.
newStr += Stringoriginal.charAt(i);
if (i == Atindex)
{
// Enter the string that needs to be entered into
// the newly created string.
newStr += InsertedString;
}
}
// return back the updated String
return newStr;
}
public static void main(String[] args)
{
String Stringoriginal = "Hello World";
String InsertedString = " Welcome To ";
int Atindex = 5;
System.out.println("The Original String given is : " + Stringoriginal);
System.out.println("The String that has to be inserted is : " + InsertedString);
System.out.println("The String that has to be inserted at the index: " + Atindex);
System.out.println("The string after insertion of another string is : " + insertString(Stringoriginal, InsertedString, Atindex));
}
}
Output:
The Original String given is : Hello World
The String that has to be inserted is : Welcome To
The String that has to be inserted at the index: 5
The string after the insertion of another string is : Hello, Welcome To World
Complexity Analysis:
The time complexity is O(n), where n is the length of the original string, because the loop iterates through every character in the string. However, because string concatenation using Java's '+' operator has an O(n) time complexity, the total time complexity increases to O(n^2) when the new string is constructed for each character in the original string and appended with the string to be inserted.
Approach: Using StringBuffer.insert() method
Algorithm:
Step 1: Obtain the index and the strings.
Step 2: Make a new StringBuffer
Step 3: Use StringBuffer.insert() for insertion of the stringToBeInserted into the original string.
Step 4: Use the StringBuffer.toString() function to return or print the string from the buffer.
Implementation:
File Name: StringInsertionUsingBuffer.java
import java.util.*;
import java.io.*;
public class StringInsertionUsingBuffer
{
// The string insertion function
public static String insertString( String Stringoriginal, String InsertedString, int Atindex)
{
// Construct a new StringBuffer.
StringBuffer newStr = new StringBuffer(Stringoriginal);
// With the insert() method,
// insert the strings that need to be inserted.
newStr.insert(Atindex + 1, InsertedString);
// return back the updated String
return newStr.toString();
}
public static void main(String[] args)
{
String Stringoriginal = "Hello World";
String InsertedString = " Welcome To ";
int Atindex = 5;
System.out.println("The Original String given is : " + Stringoriginal);
System.out.println("The String that has to be inserted is : " + InsertedString);
System.out.println("The String that has to be inserted at the index: " + Atindex);
System.out.println("The string after insertion of another string is : " + insertString(Stringoriginal, InsertedString, Atindex));
}
}
Output:
The Original String given is : Hello World
The String that has to be inserted is : Welcome To
The String that has to be inserted at the index: 5
The string after the insertion of another string is : Hello, Welcome To World
Complexity Analysis:
The code mentioned above has an O(n) time complexity, where n is the length of the original String. This is because the StringBuffer class's insert () method requires shifting characters after the insertion point in order to accommodate the new string, which results in an O(n) time complexity.
Approach: Using String.substring() method
Algorithm:
Step 1: Obtain the index and the strings.
Step 2: Construct a new String
Step 3: Use the substring (0, index+1) technique to insert the substring from 0 to the designated (index + 1).
Step 4: Next, slide the string that has to be entered into the string. Then, using the substring(index+1) method, insert the remaining portion of the original string into the new string.
Step 5: Print or Return the New String
Implementation:
File Name: StringInsertionUsingSubString.java
import java.util.*;
import java.io.*;
public class StringInsertionUsingSubString
{
// The string insertion function
public static String insertString(String Stringoriginal, String InsertedString, int Atindex)
{
// Creation of a new string
String newString = Stringoriginal.substring(0, Atindex + 1) + InsertedString + Stringoriginal.substring(Atindex + 1);
// return back the updated String
return newString;
}
public static void main(String[] args)
{
String Stringoriginal = "Hello World";
String InsertedString = " Welcome To ";
int Atindex = 5;
System.out.println("The Original String given is : " + Stringoriginal);
System.out.println("The String that has to be inserted is : " + InsertedString);
System.out.println("The String that has to be inserted at the index: " + Atindex);
System.out.println("The string after insertion of another string is : " + insertString(Stringoriginal, InsertedString, Atindex));
}
}
Output:
The Original String given is : Hello World
The String that has to be inserted is : Welcome To
The String that has to be inserted at the index: 5
The string after the insertion of another string is : Hello, Welcome To World
The Original String given is : Hello World
The String that has to be inserted is : Welcome To
The String that has to be inserted at the index: 5
The string after the insertion of another string is : Hello, Welcome To World
Complexity Analysis:
The technique mentioned above has an O(n) time complexity, where n is the length of the original String. This is due to the code's two O(n)-time usage of the substring method. Concatenating strings also requires O(n) time.