Created
June 30, 2011 03:21
-
-
Save cviebrock/1055561 to your computer and use it in GitHub Desktop.
String.reverse and String.translate
This file contains 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
String.prototype.reverse = function() { | |
var str=''; | |
for (i=this.length-1;i>=0;i--) { | |
str += this.charAt(i); | |
} | |
return str; | |
} | |
String.prototype.translate = function(from,to) { | |
var sl = this.length, | |
tl = to.length, | |
xlat = new Array(), | |
str = ''; | |
if (sl<1 || tl<1) return this; | |
for (i=0; i<256; xlat[i]=i, i++); | |
for (i=0; i<tl; i++) { | |
xlat[ from.charCodeAt(i) ] = to.charCodeAt(i); | |
} | |
for (i=0; i<sl; i++) { | |
str += String.fromCharCode( xlat[ this.charCodeAt(i) ] ); | |
} | |
return str; | |
} | |
var msg = 'Hello world!'; | |
alert(msg.reverse()); | |
alert(msg.translate('lor','gis')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment