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.

Insert a String into another String in Java

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

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(n2) 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

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. It 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

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 technique mentioned above has an O(n) time complexity, where n is the length of the original String. It is due to the code's two O(n)-time usage of the substring method. Concatenating strings also requires O(n) time.