inserting characters into a string

let`s say i have a string "abcdefg"

I want to insert 'X' between the 3rd and 4th character to make the string become "abcXdefg"

Aren`t there any function in javascript for inserting characters into a string? I tried finding and i`m surprised ther is no such function. Do i really need to write my own function for this?
[347 byte] By [hitai] at [2007-11-19 23:32:40]
# 1 Re: inserting characters into a string
I'll think you would need to make your own but it shouldn't be to difficult using substring and indexoff etc. I haven't seen one function to do that anyways :)

I'd think you could even make on using regular expression if needs be, but I'd think using substrings to build the new string would be easier.
Alsvha at 2007-11-8 0:40:38 >
# 2 Re: inserting characters into a string
yea, i agree it wouldn`t be difficult. Wonder why they didn`t add a built in function for this commonly required feature
hitai at 2007-11-8 0:41:41 >
# 3 Re: inserting characters into a string
yea, i agree it wouldn`t be difficult. Wonder why they didn`t add a built in function for this commonly required feature
Commonly required? I wouldn't exactly call it commonly required.

function addLet(let, str, after) {
// let is the X
// str is the abcdefg
// after would be 3
return str.substr(0,after) + let + str.substr(after+1);
}or if you want the much cooler prototype way:String.prototype.addLet = function(let, after) {
// let is the X
// after would be 3
return this.substr(0,after) + let + this.substr(after+1);
}Call this as "abcdefg".addLet('X',3);

I think they'll both work
Dr. Script at 2007-11-8 0:42:42 >