标签:string size slice rip color mil ice 并且 substr
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Document</title>
6 </head>
7 <body>
8 <script>
9 //concat():连接两个字符串返回一个新字符串,并且不会被修改
10 var str1 = "hello";
11 var str2 = "123";
12 var str3 = str1.concat(str2);
13 console.log(str1);
14 console.log(str2);
15 console.log(str3);
16
17 var str = "I love my family!";
18 console.log(str);
19 //slice():根据索引值和索引值截取字符串
20 console.log(str.slice(2)); //从索引截取到最后
21 console.log(str.slice(2,5)); //从索引截取,包左不包右
22 console.log(str.slice(-3)); //倒数几个
23 console.log(str.slice(5,3)); //空字符串
24
25 //substr():根据索引值和长度截取字符串
26 console.log(str.substr(2)); //从索引截取到最后
27 console.log(str.substr(2,6)); //从索引截~长度个字符串
28 console.log(str.substr(-3)); //倒数几个
29
30 //substring():根据索引和索引值截取字符串
31 console.log(str.substring(2)); //从索引截取到最后
32 console.log(str.substring(2,5)); //从索引截取~长度个字符串
33 console.log(str.substring(-1)); //全部截取
34 console.log(str.substring(5,2)); //只能调换
35 </script>
36 </body>
37 </html>
标签:string size slice rip color mil ice 并且 substr
原文地址:https://www.cnblogs.com/knuzy/p/8837541.html