Change a Character in a string - setCharAt() Function

How do you change a character in a string in JavaScript? It's not possible! So I decided to make a small function to do it. In any other language, this code will work...

var str = 'Hello World';
str[4] = ',';
alert(str);//Shows 'Hell, World'

But not in javascript. So I have come up with a simple setCharAt function that will do this for you.

function setCharAt(str,index,chr) {
	if(index > str.length-1) return str;
	return str.substr(0,index) + chr + str.substr(index+1);
}
//Demo
var str = 'Hello World';
str = setCharAt(str,4,',');
alert(str);

This method is a bit bulky. An alternative method is to extend the 'String' class using 'prototype'

String.prototype.setCharAt = function(index,chr) {
	if(index > this.length-1) return str;
	return this.substr(0,index) + chr + this.substr(index+1);
}
//Demo
var str = 'Hello World';
str = str.setCharAt(4,',');
alert(str);

A rather complicated way to do a simple thing. Am I missing something? Is there an easier way to do this?

Comments

Farben at 03 Jun, 2007 12:16
To change one char or more, without affecting the original string length :

function setChars (s, at, c) {
return s.substr(0,at) + c.substr(0,s.length-at) + s.substr(at + c.length);
}

By the way, your fonction doesn't work : your variable 'char' is a reserved word ;-)

Take care,
Farben
Reply to this.
Anonymous at 08 Apr, 2008 09:22
function setChars (s, at, c) {
return s.substr(0,at) + c.substr(0,s.length-at) + s.substr(at + c.length);
}
yours doesn't work either
Reply to this.
Anonymous at 17 Jul, 2008 01:40
Just change 'char' to 'ch' and there's no problem,
seems te be working great.

Thanks!
Reply to this.
cartlemmy at 29 Jan, 2009 10:38
I'm always astonished by how many untested functions people post :)
Reply to this.
Binny V A at 31 Jan, 2009 05:02
Sorry - I only tested it in FF. Should be fine now.
Reply to this.
Anonymous at 23 Mar, 2009 01:06
Can't beleive this can remotely beeing suggested as a reasonable solution - that's 4 objects to create in order to replace a single character!
Reply to this.
Anonymous at 12 Apr, 2009 08:55
hummm...

String.prototype.setCharAt = function(index,chr) {
if(index > this.length-1) return str; ---------------->>>>>> return this; <<<<<<<<<-----------
return this.substr(0,index) + chr + this.substr(index+1);
}
Reply to this.
Ben at 21 Sep, 2009 11:48
This will replace the value 't' with '_'

document.write('test'.replace(/'t'/g,'_'));
Reply to this.
K at 16 Nov, 2009 08:16
Here's my solution:

<script type="text/javascript">
var txt = Array("h","e","l","l","o"," ","w","o","r","l","d","!");
txt[4] = " ";
txt = txt.join("");
document.write(txt);
</script>
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.