I have a string, let\'s say Hello world
and I need to replace the char at index 3. How can I replace a char by specifying a index?
var str = \"h
There is no replaceAt
function in JavaScript. You can use the following code to replace any character in any string at specified position:
function rep() {
var str = 'Hello World';
str = setCharAt(str,4,'a');
alert(str);
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substring(0,index) + chr + str.substring(index+1);
}