How to use String slice() and Array join() Methods In Javascript
This method gets parts of a string and returns the extracted parts in a new string. Start and end parameters are used to specify the part of the string to extract. The first character starts from position 0, the second has position 1, and so on.
Syntax:
string.slice(start, end)
JavaScript Array join() Method: This method adds the elements of an array into a string and returns the string. The elements will be separated by a passed separator. The default separator is a comma (, ).
Syntax:
array.join(separator)
Example: This example inserts one string into another by using slice() and join() method.
// Input string
let str = 'GeeksGeeks'
// Input Substring
let subStr = 'for'
// Index to add substring
let pos = 5
console.log([str.slice(0, pos), subStr, str.slice(pos)].join(''))
Output
w3wiki
JavaScript Insert a string at position X of another string
Given two strings, the task is to insert one string in another at a specified position using JavaScript. We’re going to discuss a few methods, these are: