How can I insert a string at a specific index of another string?
var txt1 = \"foo baz\"
Suppose I want to insert \"bar \" after the \"foo
Here is a method I wrote that behaves like all other programming languages:
String.prototype.insert = function(index, string) {
if (index > 0) {
return this.substring(0, index) + string + this.substr(index);
}
return string + this;
};
//Example of use:
var something = "How you?";
something = something.insert(3, " are");
console.log(something)