Skip to content

Instantly share code, notes, and snippets.

@najlepsiwebdesigner
Last active December 13, 2015 19:58
Show Gist options
  • Save najlepsiwebdesigner/4966627 to your computer and use it in GitHub Desktop.
Save najlepsiwebdesigner/4966627 to your computer and use it in GitHub Desktop.
javascript insert([index],[string]) method (String.prototype.insert)
// String insert method
String.prototype.insert = function (index, string) {
if (index > 0) {
// necessary fix
if (string.charCodeAt(0) == 13){
string = "\n";
}
var ret = this.substring(0, index) + string + this.substring(index, this.length);
return ret;
} else if (index == this.length) {
return this + string;
} else {
return string + this;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment