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:

Methods to Insert String at a Certain Index

Table of Content

  • Method 1: Using JavaScript String slice() and Array join() Methods
  • Method 2: JavaScript String substr() Method
  • Method 3: Using JavaScript splice() method
  • Method 4: Using JavaScript String concat() Method
  • Method 5: Using String.substring() Method

Method 1: Using JavaScript String slice() and Array join() Methods

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.

Javascript
// Input string
let str = 'BeginnerBeginner'

// 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

Method 2: JavaScript String substr() Method

This method gets parts of a string, starting at the character at the defined position, and returns the specified number of characters. 

Syntax:

string.substr(start, length)

Example: This example inserts one string to another by using substr() method.

Javascript
// Input string
let str = 'BeginnerBeginner';

// Input Substring
let subStr = 'for';

// Given index
let pos = 5;

// Insert and display output
console.log(str.substr(0, pos) + subStr + str.substr(pos));

Output
w3wiki

Method 3: Using JavaScript splice() method

The JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

Syntax:

Array.splice( index, remove_count, item_list )

Example:

Javascript
// Input string
let str = 'BeginnerBeginner'

// Input Substring
let subStr = 'for'

// Index to add substring
let pos = 5

// Convert to array of string
let arr = str.split('')

// Add substring at given position
arr.splice(pos, 0, ...subStr)

// Display result
console.log(arr.join(''))

Output
w3wiki

Method 4: Using JavaScript String concat() Method

The concat() method concatenates the string arguments to the calling string and returns a new string.

Syntax:

string.concat(string2, string3, ..., stringN)

Example: This example demonstrates how to insert one string into another using the concat() method by breaking the main string into two parts and then concatenating them with the substring in between.

JavaScript
// Input string
let str = 'BeginnerBeginner';

// Input Substring
let subStr = 'for';

// Index to add substring
let pos = 5;

// Using concat() to insert the substring
let result = str.slice(0, pos).concat(subStr, str.slice(pos));

console.log(result);

Output
w3wiki

Method 5: Using String.substring() Method

The substring() method extracts the characters from a string between two specified indices and returns a new string. We can utilize this method to split the original string into two parts at the specified index, then concatenate the substring in between.

Syntax:

string.substring(startIndex, endIndex)
  • startIndex: The index at which to begin extraction. If negative, it is treated as str.length + startIndex. (For example, if startIndex is -3, it is treated as str.length – 3.)
  • endIndex: The index at which to end extraction. If omitted, the slice goes to the end of the string. If negative, it is treated as str.length + endIndex. (For example, if endIndex is -3, it is treated as str.length – 3).

Example:

JavaScript
// Input string
let str = 'BeginnerBeginner';

// Input Substring
let subStr = 'for';

// Index to add substring
let pos = 5;

// Using substring() to insert the substring
let result = str.substring(0, pos) + subStr + str.substring(pos);

console.log(result);

Output
w3wiki