join(' ,')或join(' - ')或join(' 。')
##中間這個逗號是手動加裝的,也可以改成別的例如、。 ! -等等都可以 |
| #// join()
var a= ["00", "01", "02", "03", "04"]
console.log(a)
var b= a.join()
console.log(b)
console.log( typeof b)
//打印结果 00,01,02,03,04
登入後複製
// join('')
var a= ["00", "01", "02", "03", "04"]
console.log(a)
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"]
console.log(a)
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
登入後複製
var a= ["00", "01", "02", "03", "04"]
console.log(a)
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"]
登入後複製
###【相關推薦:###javascript影片教學###、###程式設計影片###】###