JavaScript | Insert a string at position X of another string

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.

  • 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 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.

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      • Before clicking on the button:
      • After clicking on the button:

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

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      • Before clicking on the button:
      • After clicking on the button:


      My Personal Notes arrow_drop_up

      Check out this Author's contributed articles.

      If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

      Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.