In this article, we are given a string containing words and the task is to insert a new string at a given index. There are some methods to solve this problem which are discussed below:
Methods to Insert String at a Specific index:
- Using the JavaScript slice() method
- Using the JavaScript splice() method
- Using JavaScript substring() method
Method 1: Using the slice() method.
The string is first divided into two parts by splitting it where the new string has to be inserted.
The slice() method is used to return an array between two indices. It takes two parameters, one is the beginning index which specifies and the other is the ending index.
The first part of the string is extracted using the starting position as 0 and the ending position as the index where the new string has to be inserted. The second part of the string is extracted using the starting position as the index where the new string has to be inserted.
The ending position is optional here and it will be assumed till the end of the string. The new string is added in between these two parts by simple string concatenation.
Example: This example shows the above-explained approach.
Javascript
// Input Stringlet origString = "GeeksGeeks";// String to be addedlet stringToAdd = "For";// Position to add stringlet indexPosition = 5;// Using slice method to split stringnewString = origString.slice(0, indexPosition) + stringToAdd + origString.slice(indexPosition);// Display outputconsole.log(newString); |
GeeksForGeeks
Method 2: Using the splice() method.
The splice() method is used to insert or replace the contents of an array at a specific index. This can be used to insert the new string at the position of the array. It takes 3 parameters, the index where the string is to be inserted, the number of deletions to be performed if any, and the string to be inserted.
The required index and the string are passed as parameters to this method with the deletions parameter set to 0. It inserts the string into the array. The array is joined back using the join() method. This makes it one whole string again with the new string joined in between.
Example: This example shows the above-explained approach.
Javascript
// Input Stringlet origString = "GeeksGeeks";// String to be addedlet stringToAdd = "For";// Position to add stringlet indexPosition = 5;// Split the string into individual// charactersorigString = origString.split(''); // Insert the string at the index positionorigString.splice(indexPosition, 0, stringToAdd); // Join back the individual characters// to form a new stringnewString = origString.join(''); // Display outputconsole.log(newString); |
GeeksForGeeks
Method 3: Using JavaScript substring() method
The string.substring() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing start from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Example: In this example, we will split the string using substring method and add the given string at the given index.
Javascript
// Input Stringlet origString = "GeeksGeeks";// String to be addedlet stringToAdd = "For";// Position to add stringlet indexPosition = 5;// Using substring method to split stringnewString = origString.substring(0, indexPosition) + stringToAdd + origString.substring(indexPosition);// Display outputconsole.log(newString); |
GeeksForGeeks
