Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Created December 3, 2015 13:01
Show Gist options
  • Save SeanJM/9e6374d42496054c87d7 to your computer and use it in GitHub Desktop.
Save SeanJM/9e6374d42496054c87d7 to your computer and use it in GitHub Desktop.
A function to splice strings.
// 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