Javascript 权威指南上说:
When we set out to write the code to perform this experiment, however, we run into a major stumbling block: there is no way to modify the contents of a string. The charAt( ) method returns the character at a given position in a string, but there is no corresponding setCharAt( ) method. This is not an oversight. JavaScript strings are intentionally immutable -- that is, there is no JavaScript syntax, method, or property that allows you to change the characters in a string.
replace()
The replace() method is used to replace some characters with some other characters in a string.replace() 难道不是修改string 方法吗?
When we set out to write the code to perform this experiment, however, we run into a major stumbling block: there is no way to modify the contents of a string. The charAt( ) method returns the character at a given position in a string, but there is no corresponding setCharAt( ) method. This is not an oversight. JavaScript strings are intentionally immutable -- that is, there is no JavaScript syntax, method, or property that allows you to change the characters in a string.
replace()
The replace() method is used to replace some characters with some other characters in a string.replace() 难道不是修改string 方法吗?
看名字就知道是替换
所以你无法用SetCharat()来修改.
Replace()其实是创建了一个新的String