Created
December 3, 2015 13:01
-
-
Save SeanJM/9e6374d42496054c87d7 to your computer and use it in GitHub Desktop.
A function to splice strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage : stringSplice('my string', 2, 0, ' awesome'); | |
// -> 'my awesome string' | |
// | |
// How to extend the native string prototype (if you want): | |
// String.prototype.splice = function () { | |
// var args = [].slice.call(arguments); | |
// return stringSplice.apply(null, [this.valueOf()].concat(args)); | |
// }; | |
function stringSplice(baseString, start, length, newString) { | |
var a = baseString.substr(0, start); | |
var b = baseString.substr(start + length, baseString.length - start - length); | |
return a + newString + b; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment