Latest Post

Error: No input file specified.

While trying to run a codeigniter app the other day I came across this error just after I had finished uploading the files and making the ne...

Wednesday, February 2, 2011

Javascript: reversing a string


function strrev(str)
{
if (!str) return '';
var revstr='';
for (i = str.length-1; i>=0; i--)
revstr+=str.charAt(i)
return revstr;
}

alert(strrev("Hello World!"));


OR alternatively you can use split("") to
convert a string to an array, reverse() to
reverse the array, and join("") to convert
it back.

function strrev(str)
{
return str.split("").reverse().join("");
}

alert(strrev("Hello World!"));

2 comments:

Chitika Ads