JavaScript 字符串与列表的相互转化 ,使用split 与join 方法。。
应用--翻转字符串,把hello world. 变成 dlrow. olleh
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 对“hello world”进行翻转处理 要求变为:"dlorw olleh"
function reserve_str() {
var alp = 'hello world'
var r_alp = alp.split("")
// console.log(r_alp) // ['d','l','r','o'..'e','h']
r_alp.reverse() //反转
var r_str = r_alp.join('') //拼接为字符串
return r_str
}
var s = reserve_str()
alert(s) // 'dlrow olleh'
</script>
</body>
</html>