04字符串拓展-es6
# 字符串拓展
//for of 字符串遍历
let str = "hello world"
for(s of str){
console.log(s)
}
//includes 返回布尔值,表示是否找到了参数字符串
let str = "hello world"
console.log(str.includes("hello"))//true
//repeat 方法返回一个新字符串,表示将原字符串重复n次
let str = "hello"
console.log(str.repeat(2))//hellohello
//模版字符串
let str = `hello world`
let str = `飞流直
下三千尺`
console.log(str)
let s = "world"
let str = "hello"+s
console.log(str)
let s = "world"
let str = `hello ${s}`
console.log(str)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[!cogs]
%accordion% for of 字符串遍历 %accordion%
let str = "hello world"
for (idx of str) {
console.log(idx);
}
1
2
3
4
2
3
4
%/accordion%
# 模版字符串
let str = `hello
world`
console.log(str);
//答案: //hello
//world
let a = "hello"
let b = "world"
console.log(`${a}${b}`); //helloworld
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
编辑 (opens new window)
上次更新: 2022/04/24, 13:33:34