1

I have a string. It's just like;

var str = "This is an example sentence, thanks.";

I want to change every fifth element of this string.

for(var i=5; i<=str.length; i=i+5)
{
   str[i]='X';  // it doesn't work, I need something like that
}

So I want str to be "ThisXis aX exaXple XenteXce, XhankX."

Is there any way to do that?

Rckt
  • 185
  • 1
  • 3
  • 11
  • have you tried with [concat()](http://www.w3schools.com/jsref/jsref_concat_string.asp) and [substring()](http://www.w3schools.com/jsref/jsref_substring.asp)? – dani herrera Dec 28 '11 at 20:54
  • Could you explain a bit. – Rckt Dec 28 '11 at 20:54
  • Here is related discussion http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript – kosa Dec 28 '11 at 21:01

5 Answers5

3

Use RegEx

str = str.replace(/(....)./g, "$1X")

jsfiddle

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
2
var str = "This is an example sentence, thanks.";

var newString = "";
for(var i=0; i<str.length; i++)
{
   if ((i % 5) == 4) {
     newString += "X";
   } else {
     newString += str.charAt(i);
   }
}

Here is a running example. http://jsfiddle.net/WufuK/1

John Hartsock
  • 85,422
  • 23
  • 131
  • 146
2

You can use string.substring()

var a = "This is an example sentence, thanks.";
var result ="";
for(var i=0;i <a.length-1; i+=5){
    result +=a.substr(i, 4)+'X';
}

alert(result)

jsFiddle: http://jsfiddle.net/mVb4u/5

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
  • You may want to take another look at the result in your Fiddle. You should be doing `a.substr(i, 4)+'X'` or `a.substring(i, i+4)+'X'`. –  Dec 28 '11 at 21:15
2

You could use a map, although the regex solution looks better.

str = str.split('').map(function(chr, index){
  return (index % 5 === 4)? 'X' : chr;
}).join('');
david
  • 17,925
  • 4
  • 43
  • 57
2

This approach uses Array.reduce, which is native to JavaScript 1.8, but can be backported.

Array.prototype.reduce.call("This is an example sentence, thanks.", function(p,c,i,a) { return p + ( i % 5 == 4 ? "X" : c); });

Update: Updated to reflect am not i am's comments below.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • You probably mean `Array.prototype.reduce.call("This is...)` –  Dec 28 '11 at 21:08
  • @amnotiam no, I actually meant what I wrote. Your method would work in older versions of JavaScript, but [Array generics](https://developer.mozilla.org/en/New_in_JavaScript_1.6#Array_and_String_generics) are available in 1.6 and up. – kojiro Dec 28 '11 at 21:53
  • 1
    Well the link in your question links to the compatibility patch for `Array.prototype.reduce`, not `Array.reduce`. Would be a good idea to note that you're referring to that non-ES-standard Mozilla extension. The closest it is to standard right now seems to be [this strawman proposal](http://wiki.ecmascript.org/doku.php?id=strawman:array_statics). –  Dec 28 '11 at 22:05
  • 1
    @amnotiam Good point, I was misled by the MDN. Normally when something is nonstandard they point it out. – kojiro Dec 28 '11 at 22:19