Here given a string and the task is to insert a character after every n characters in that string. Here are 2 examples discussed below.
Approach 1: In this approach, the string is broken into chunks by using substr() method and pushed to an array by push() method. The array of chunks is returned which then joined using join() method on any character.
Example:
<!DOCTYPE HTML> <html> <head> <title> Insert a character after every n characters in JavaScript </title> <script src= </script> </head> <body style = "text-align:center;"> <h1 style = "color: green"> GeeksForGeeks </h1> <p id = "GFG_UP"> </p> <button onclick = "gfg_Run()"> Click Here </button> <p id = "GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "A computer science portal for Geeks"; el_up.innerHTML = "Click on the button to perform the operation.<br>'" + str + "'"; function partition(str, n) { var retrn = []; var i, l; for(i = 0, l = str.length; i < l; i += n) { retrn.push(str.substr(i, n)); } return retrn; }; function gfg_Run() { el_down.innerHTML = partition(str, 5).join('@'); } </script> </body> </html> |
Output:

Approach 2: In this approach, a RegExp is used which selects the parts of string and then joined on any character using join() method.
Example:
<!DOCTYPE HTML> <html> <head> <title> Insert a character after every n characters in JavaScript </title> <script src= </script> </head> <body style = "text-align:center;"> <h1 style = "color: green"> GeeksForGeeks </h1> <p id = "GFG_UP"> </p> <button onclick = "gfg_Run()"> Click Here </button> <p id = "GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var str = "A computer science portal for Geeks"; el_up.innerHTML = "Click on the button to perform the operation.<br>'" + str + "'"; function gfg_Run() { el_down.innerHTML = str.match(/.{1, 5}/g).join('@'); } </script> </body> </html> |
Output:

Recommended Posts:
- How to insert a string at a specific index in JavaScript ?
- How to insert a JavaScript variable inside href attribute?
- How to insert an item into array at specific index in JavaScript?
- What is the Efficient way to insert a number into a sorted array of numbers in JavaScript?
- How to get the last character of a string in JavaScript?
- How to remove a character from string in JavaScript ?
- How to find out which character key is pressed using JavaScript?
- How to get character array from string in JavaScript?
- How to replace a character at a particular index in JavaScript ?
- Delete first character of a string in JavaScript
- JavaScript SyntaxError - Illegal character
- JavaScript to keep only first N characters in a string
- How to replace lowercase letters with an other character in JavaScript ?
- How to remove portion of a string after certain character in JavaScript ?
- JavaScript | Strip all non-numeric characters from string
- How to convert special characters to HTML in Javascript?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- JavaScript | Insert a string at position X of another string
- Replace special characters in a string with underscore (_) in JavaScript
- PHP | Ds\Sequence insert() Function
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.


