How to use String Concatenation In Javascript
This method utilizes string concatenation to insert a new string at a specific index. It involves splitting the original string into two parts based on the insertion index and then concatenating the new string in between these parts.
Example: In this example, we will split the string using concatenation.
// Input String
let origString = "GeeksGeeks";
// String to be added
let stringToAdd = "For";
// Position to add string
let indexPosition = 5;
// Construct the new string by concatenating the substrings
let newString = origString.substring(0, indexPosition) + stringToAdd + origString.substring(indexPosition);
// Display output
console.log(newString);
Output
w3wiki
How to insert a string at a specific index in JavaScript ?
In this article, we are given a string containing words and the task is to insert a new string at a given index. Inserting string at a particular position means increasing the length of the original string.There are some methods like slice( ), splice( ), and substring( ) to solve this problem which are discussed below:
Table of Content
- Method 1: Using the slice() method
- Method 2: Using the splice() method
- Method 3: Using JavaScript substring() method
- Method 4: Using String Concatenation
Contact Us