Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1658 How do I automatically insert a character after every Nth character in a string?

You are here: irt.org | FAQ | JavaScript | Text | Q1658 [ previous next ]

Try:

<script language="JavaScript"><!--
function insertNthChar(string,chr,nth) {
  var output = '';
  for (var i=0; i<string.length; i++) {
    if (i>0 && i%4 == 0)
      output += chr;
    output += string.charAt(i);
  }

  return output;
}

alert(insertNthChar('erty1f213dfs1111', '-', 4));
//--></script>

Feedback on 'Q1658 How do I automatically insert a character after every Nth character in a string?'

©2018 Martin Webb