Home >Web Front-end >JS Tutorial >How to reverse string in javascript_javascript skills

How to reverse string in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:15:331300browse

The example in this article describes how to achieve string reversal in JavaScript. Share it with everyone for your reference. The specific implementation method is as follows:

//变量交换 方法
var a=0,b=1,c=2;

a=[b,b=c,c=a][0];

//正文
var array='abcdefgh';
function Reverse(arr){
     var arr1=arr.split('');//[a,b,c,d,e,f,g]
     var halfLen=Math.floor(arr.length/2);//3
     alert(halfLen)
     var len=arr.length;//7
     var s=0;
     for(s=0;s<=halfLen;s++){
       arr1[s]=[arr1[len-s],arr1[len-s]=arr1[s]][0]
     }
     alert(arr1.join(''))
}
Reverse(array)

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
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