Home > Web Front-end > JS Tutorial > Three methods to achieve string reversal in javascript_javascript skills

Three methods to achieve string reversal in javascript_javascript skills

WBOY
Release: 2016-05-16 17:12:41
Original
1818 people have browsed it

The first method

Copy code The code is as follows:

var str = "abcdef";
console.log( str.split("").reverse().join("") );

Second method:
Copy code The code is as follows:

var str="abcdef"
var i=str.length;
i=i- 1;
for (var x = i; x >=0; x--)
{
document.write(str.charAt(x));
}

Third method:
Copy code The code is as follows:


<script> <br>function reverse(str) <br>{ <br> if(str.length == 0)return null; <br> var i = str. length; <br> var dstr = ""; <br> while(--i >= 0) <br> { <br> dstr = str.charAt(i); <br> } <br> return dstr; <br>} <br>var str = "abcdef"; <br>str = reverse(str); <br>document.write(str); <br></script>


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template