Skip to content

Instantly share code, notes, and snippets.

@cristiandley
Created November 7, 2014 16:48
Show Gist options
  • Save cristiandley/2021c5a2bfcfb1282e2f to your computer and use it in GitHub Desktop.
Save cristiandley/2021c5a2bfcfb1282e2f to your computer and use it in GitHub Desktop.
Replace an specific kind of chars
/**
* REPLACE ALL CHARS
* (but the name is not.... i know!)
*/
String.prototype.replaceChar=function(position, char) {
return this.substr(0, position) + char + this.substr(position, this.length);
}
String.prototype.replaceAllChars = function(thisChar, forThisChar){
var lngChar = forThisChar.length;
var _tmp = this;
for (var position=0; position < _tmp.length; position++){
if(_tmp[position] == thisChar){
_tmp = _tmp.replaceChar(position, forThisChar);
position = position+(lngChar+1);
}
}
return _tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment