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

Share 2 ways to implement string flipping in js

小云云
小云云Original
2018-03-07 11:04:151679browse

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!

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