<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> 数组分割字符串 </title>
</head>
<body>
<script>
// 数组转字符串
const liang = ['h', 'e', 'l', 'l', 'o']
console.log("原数组:", liang) // 原数组: (5) ["h", "e", "l", "l", "o"]
const str = liang.map((item) => item).join('')
console.log("转成的字符串:", str) // 转成的字符串: hello
// 字符串转数组
const liang1 = "hello"
console.log("原字符串:", liang1) // 原字符串: hello
const arr1 = str.split('')
console.log("转成的数组:", arr1) // 转成的数组: (5) ["h", "e", "l", "l", "o"]
// 字符串的切割
// 原字符串为hello, word 想截取word,
const liang2 = "hello,word"
console.log("原字符串:", liang2) // 原字符串: hello,word
const arr2 = liang2.split(',')
console.log("转成的数组:", arr2) // 转成的数组: (2) ["hello", "word"]
console.log("转成的数组:", arr2[0]) // 转成的数组: hello
console.log("转成的数组:", arr2[1]) // 转成的数组: word
</script>
</body>
</html>
网友评论