JavaScript | Insert a string at position X of another string - GeeksforGeeks

03 Jun, 2019
Given 2 string, the task is to insert one string in another at a specified position using javascript, we’re going to discuss few techniques.First few methods to know.
JavaScript String slice() method: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.First character starts from position 0, the second has position 1, and so on.Syntax:string.slice(start, end)
Parameters:start: This parameter is required. It specifies the position from where to start the extraction. The first character starts from position 0.end: This parameter is optional. It specifies the position (excluding it) where to stop the extraction. If not used, slice() selects all characters from the start-position to the end.Return value:It returns a string, representing the extracted part of the string.
string.slice(start, end)

Parameters:
start: This parameter is required. It specifies the position from where to start the extraction. The first character starts from position 0.
end: This parameter is optional. It specifies the position (excluding it) where to stop the extraction. If not used, slice() selects all characters from the start-position to the end.
Return value:It returns a string, representing the extracted part of the string.
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)
Parameters:separator: This parameter is optional. It specifies the separator to be used. If not used, the elements are separated with a commaReturn value:It returns a string, denoting the array values, separated by the defined separator.
array.join(separator)

Parameters:
separator: This parameter is optional. It specifies the separator to be used. If not used, the elements are separated with a comma
Return value:It returns a string, denoting the array values, separated by the defined separator.
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)
Parameters:start: This parameter is required. It specifies the position from where to begin the extraction. First character is at index 0.If start is positive and greater than, or equal, to the length of the string, this method returns an empty string.If start is negative, this method uses it as an index from the end.If start is negative or larger than the length of the string, start is used as 0.length: This parameter is optional. It specifies the number of characters to extract. If not used, it extracts the whole string.Return value:it returns a new string, having the extracted part of the text. If length is 0 or negative, an empty string is returned.Example 1: This example inserts one string to another by using slice() and join() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Insert string at position X of another string.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 19px;              font-weight: bold;">    </p>    <button onclick="GFG_Fun(); ">        click here    </button>    <p id="GFG_DOWN"        style="color: green;               font-size: 24px;               font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var a = 'GeeksGeeks';        var b = 'For';        var pos = 5;        up.innerHTML = 'Str_1 = "' + a +           '"<br>Str_2 = "' + b + '"';          function GFG_Fun() {            down.innerHTML =               [a.slice(0, pos), b, a.slice(pos)].join('')        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:Example 2: This example inserts one string to another by using substr() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Insert string at position X of another string.    </title></head>  <body style="text-align:center;"       id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 19px;              font-weight: bold;">    </p>    <button onclick="GFG_Fun(); ">        click here    </button>    <p id="GFG_DOWN"        style="color: green;              font-size: 24px;              font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var a = 'GeeksGeeks';        var b = 'For';        var pos = 5;        up.innerHTML = 'Str_1 = "' + a         + '"<br>Str_2 = "' + b + '"';          function GFG_Fun() {            down.innerHTML =               a.substr(0, pos) + b + a.substr(pos);        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:My Personal Notes
arrow_drop_upSave
string.substr(start, length)

Parameters:
start: This parameter is required. It specifies the position from where to begin the extraction. First character is at index 0.If start is positive and greater than, or equal, to the length of the string, this method returns an empty string.If start is negative, this method uses it as an index from the end.If start is negative or larger than the length of the string, start is used as 0.
length: This parameter is optional. It specifies the number of characters to extract. If not used, it extracts the whole string.
Return value:it returns a new string, having the extracted part of the text. If length is 0 or negative, an empty string is returned.
Example 1: This example inserts one string to another by using slice() and join() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Insert string at position X of another string.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 19px;              font-weight: bold;">    </p>    <button onclick="GFG_Fun(); ">        click here    </button>    <p id="GFG_DOWN"        style="color: green;               font-size: 24px;               font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var a = 'GeeksGeeks';        var b = 'For';        var pos = 5;        up.innerHTML = 'Str_1 = "' + a +           '"<br>Str_2 = "' + b + '"';          function GFG_Fun() {            down.innerHTML =               [a.slice(0, pos), b, a.slice(pos)].join('')        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Example 2: This example inserts one string to another by using substr() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Insert string at position X of another string.    </title></head>  <body style="text-align:center;"       id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 19px;              font-weight: bold;">    </p>    <button onclick="GFG_Fun(); ">        click here    </button>    <p id="GFG_DOWN"        style="color: green;              font-size: 24px;              font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var a = 'GeeksGeeks';        var b = 'For';        var pos = 5;        up.innerHTML = 'Str_1 = "' + a         + '"<br>Str_2 = "' + b + '"';          function GFG_Fun() {            down.innerHTML =               a.substr(0, pos) + b + a.substr(pos);        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript-Misc
javascript-string
HTML
JavaScript
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
CSS to put icon inside an input element in a form
Types of CSS (Cascading Style Sheet)
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?