一:數組轉字元串(3種方法)
同樣是數組轉字元串,toString(),toLocaleString(),join(),join(‘,’)的區別是什麼?
JavaScript 允許數組與字元串之間相互轉換。其中 Array 方法對象定義了 3 個方法,可以把數組轉換為字元串,如表所示。
| 數組方法 | 說明 |
| toString() | 將數組轉換成一個字元串 |
| toLocaleString() | 把數組轉換成本地約定的字元串 |
| join() | 將數組元素連接起來以構建一個字元串 |
1:join()方法用於把數組中的所有元素放入一個字元串
元素是通過指定的分隔符進行分隔的
| join()指定的分隔符 | 說明 |
| join() | 可理解為直接變成字元串,默認逗號分隔 |
| join(‘ ‘) | 空連接 |
| join(‘ ,’)或者 join(‘ – ‘)或者 join(‘ 。’) | 中間這個逗號是手動添加的,也可以改成別的比如、。! -等等都可以 |
// join()
var a= ["00", "01", "02", "03", "04"]
var b= a.join()
console.log(b)
console.log( typeof b)
//列印結果 00,01,02,03,04
// join('')
var a= ["00", "01", "02", "03", "04"]
var b= a.join('')
console.log(b)
console.log( typeof b)
//列印結果 0001020304
// join(',')
var a= ["00", "01", "02", "03", "04"]
var b= a.join(',')
console.log(b)
console.log( typeof b)
//列印結果 00,01,02,03,04
或者
// join('-')
var a= ["00", "01", "02", "03", "04"]
var b= a.join('-')
console.log(b)
console.log( typeof b)
//列印結果 00-01-02-03-04
或者
// join('!')
var a= ["00", "01", "02", "03", "04"]
var b= a.join('!')
console.log(b)
console.log( typeof b)
//列印結果 00!01!02!03!04
2:toString()方法可把一個邏輯值轉換為字元串,並返回結果
var a= ["00", "01", "02", "03", "04"]
var c = a.toString(); //把數組轉換為字元串
console.log(c)
console.log(typeof c); //返回字元串string,說明是字元串類型
//列印結果 00,01,02,03,04
toString()方法不可以指定分隔符,但是我們可以通過replace()方法指定替換
var a= ["00", "01", "02", "03", "04"]
var f = a.toString().replace(/,/gi,'-')
console.log(f)
//列印結果:00-01-02-03-04
3:toLocaleString()
把數組轉換成本地約定的字元串
var a= ["00", "01", "02", "03", "04"]
var e = a.toLocaleString();
console.log(e)
console.log(typeof e);
//列印結果:00,01,02,03,04
demo
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var a= ["00", "01", "02", "03", "04"]
//1:
var b= a.join(',')
console.log(b)
console.log( typeof b)
//2:
var c = a.toString(); //把數組轉換為字元串
console.log(c)
console.log(typeof c); //返回字元串string,說明是字元串類型
//3:
var d = a.join(); //把數組轉換為字元串
console.log(d)
console.log(typeof d); //返回字元串string,說明是字元串類型
//4:
var e = a.toLocaleString(); //把數組轉換為字元串
console.log(e)
console.log(typeof e); //返回字元串string,說明是字元串類型
</script>
</html>

二:字元串轉數組(2種方法)
| 字元串方法 | 說明 |
| split() 方法 | 將字元串轉換成一個數組 |
| 擴展運算符(…) | es6裡面的擴展運算符 |
字元串轉數組
1:split() 方法用於把一個字元串分割成字元串數組
同樣是用於把一個字元串分割成字元串數組,split(‘,’),split(),split(‘ ‘)的區別是什麼?
| split()方法 | 說明 |
| split(‘,’) | |
| split() | 可理解為直接變成字元串,默認逗號分隔 |
| split(‘ ‘) 空字元串 | 每個字元之間都會被分割 |
var arr = 'aa,bb,cc,dd'
var newStr = arr.split(',')
console.log(newStr)
// 列印結果:["aa", "bb", "cc", "dd"]
var arr = 'aa,bb,cc,dd'
var newStr = arr.split()
console.log(newStr)
// 列印結果: ["aa,bb,cc,dd"]
如果把空字元串 (“”) 用作 separator,那麼 stringObject 中的每個字元之間都會被分割
var arr = 'aa,bb,cc,dd'
var newStr = arr.split('')
console.log(newStr)
//列印結果: ["a", "a", ",", "b", "b", ",", "c", "c", ",", "d", "d"]
2:es6裡面的擴展運算符
var arr = 'aa,bb,cc,dd'
var newStr = [...arr]
console.log(newStr)
//列印結果 ["a", "a", ",", "b", "b", ",", "c", "c", ",", "d", "d"]
以上就是js數組與字元串相互轉換的幾種方法啦~∠( °ω°)/ ~
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/274874.html
微信掃一掃
支付寶掃一掃