JavaScript - Insert a String at a Specific Index Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following methods to insert a string at a specific index in JavaScript:1. Using the slice() MethodInserting a string using the slice() method involves splitting the original string into two parts: one before the insertion point and one after. The new string is then placed between these slices, effectively inserting it at the desired index. JavaScript let str = "GeeksGeeks"; let str2 = "For"; let idx = 5; let res = str.slice(0, idx) + str2 + str.slice(idx); console.log(res); OutputGeeksForGeeks 2. Using JavaScript substring() MethodInserting a string using the substring() method involves splitting the original string into two parts based on the index, inserting the new string between these parts, and then concatenating them. This modifies the string with the desired insertion at the specified position. JavaScript let str = "GeeksGeeks"; let str2 = "For"; let idx = 5; let res = str.substring(0, idx) + str2 + str.substring(idx); console.log(res); OutputGeeksForGeeks 3. Using Regular ExpressionInserting a string using a Regular Expression involves matching a specific position or pattern in the original string and using the replace() method to insert the new string at the desired location within the matched pattern. JavaScript let str = 'hello'; let res = str.replace(/(.{3})/, '$1***'); console.log(res); Outputhel***lo 5. Using Template LiteralsTemplate literals provide a straightforward and readable way to insert a new string at a specified index. By breaking the original string into two parts and using template literals, you can easily insert the new string. JavaScript const str = "Hello, World!"; const str1 = " Amazing"; const idx = 7; const s1 = str.slice(0, idx); const s2 = str.slice(idx); const res = `${s1}${str1}${s2}`; console.log(res); OutputHello, AmazingWorld! 6. Using Array Spread OperatorIn this approach, we will convert the string to an array of characters, use the array spread operator to insert the new string at the desired index, and then join the array back into a single string. This method is efficient and leverages modern JavaScript syntax for clarity and conciseness. JavaScript let str = "HelloWorld"; let str1 = "Beautiful"; let idx = 5; let a = [...str]; a.splice(idx, 0, ...str1); let res = a.join(''); console.log(res); OutputHelloBeautifulWorld 7. String Insertion Using LoopsInserting a string using loops involves iterating through the original string until the specified index is reached, then adding the new string. The loop continues with the remaining part of the original string, constructing a new string with the inserted content. JavaScript function insertAt(str, str1, idx) { if (idx < 0 || idx > str.length) { return str; // or throw an error } let res = ''; let i = 0; while (i < idx) { res += str[i]; i++; } let j = 0; while (j < str1.length) { res += str1[j]; j++; } while (i < str.length) { res += str[i]; i++; } return res; } let str = "Hello World!"; let str1 = "JavaScript "; let idx = 6; console.log(insertAt(str, str1, idx)); OutputHello JavaScript World! Comment More infoAdvertise with us Next Article JavaScript - Insert a String at a Specific Index sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA Similar Reads How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read Reverse a String in JavaScript We have given an input string and the task is to reverse the input string in JavaScript. Reverse a String in JavaScriptUsing split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characte 1 min read JavaScript - Convert String to Title Case Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript.1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and t 4 min read JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana', 3 min read How to Convert String to Camel Case in JavaScript? We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin 4 min read Extract a Number from a String using JavaScript We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.Below are the methods to extract a number from string using JavaScript:Table of ContentUsing JavaScript match method with regExUsing 4 min read JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s 1 min read JavaScript - How to Get Character Array from String? Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf 2 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read Like