Home >Web Front-end >JS Tutorial >Share 2 ways to implement string flipping in js
We have shared with you many ways to implement string flipping in PHP. This article mainly shares with you two ways to implement string flipping in js. I hope it can help you.
1. Complete flipping of the string:
var str = "smile at life"; document.write(str.split("").reverse().join("")); //结果为efil ta elims
2. Flip the order of words in the string, but the alphabetical order of the words remains unchanged:
function reverseStr(param){ var arr = param.split(" "); var newArr = []; for(i=0;i<arr.length;i++){ newArr[arr.length-i] = arr[i]; } return newArr.join(" "); } document.write(reverseStr("smile at life")); //life at smile
Related recommendations:
How to implement string flip function in PHP
The above is the detailed content of Share 2 ways to implement string flipping in js. For more information, please follow other related articles on the PHP Chinese website!