Home > Web Front-end > JS Tutorial > Share 2 ways to implement string flipping in js

Share 2 ways to implement string flipping in js

小云云
Release: 2018-03-07 11:04:15
Original
1796 people have browsed it

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
Copy after login

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
Copy after login

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!

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