0
string[index] = 'a'

seems didn't work, it cannot change the string. Why's that and are there any articles about this?

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • check out splice function http://www.w3schools.com/jsref/jsref_splice.asp – Nilzone- Dec 08 '13 at 21:37
  • please read more about strings https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String – Jaak Kütt Dec 08 '13 at 21:38
  • Related: [Are JavaScript strings immutable? Do I need a “string builder” in JavaScript?](http://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript) and [What does immutable mean?](http://stackoverflow.com/questions/3200211/what-does-immutable-mean) – apsillers Dec 08 '13 at 21:40

2 Answers2

0

here an example of a function that would solve this

function replaceAt(string, index, newValue) {
  if(index >= string.length || index < 0) {return false;}
  var start = string.substr(0,index);
  var finish = string.substr(index+1);
  return start + newValue.toString() + finish;
}
0

Strings are not arrays but you can convert them into arrays and then join them back into strings.

var strArray = string.split("");
strArray[index] = 'a';
string = strArray.join("");
Elie
  • 6,915
  • 7
  • 31
  • 35