1 //字符串扩展
 2 {
 3     console.log('a','\u0061');
 4     console.log('s','\u20BB7');//超过了0xffff
 5 
 6     console.log('s','\u{20BB7}');//如果超过就用{}包裹
 7 
 8 }
 9 {
10     //es5中
11     let s='𠮷';
12     console.log('length',s.length);//length 2
13     console.log('0',s.charAt(0));//0 �
14     console.log('1',s.charAt(1));//1 �
15     console.log('at0',s.charCodeAt(0));//at0 55362
16     console.log('at1',s.charCodeAt(1));//at1 57271
17 
18     //es6中
19     let s1='𠮷a';
20     console.log('length',s1.length);//length 3
21     console.log('code0',s1.codePointAt(0));//code0 134071
22     console.log('code0',s1.codePointAt(0).toString(16));//code0 20bb7
23     console.log('code1',s1.codePointAt(1));//code1 57271
24     console.log('code2',s1.codePointAt(2));//code2 97
25 }
26 
27 {
28     console.log(String.fromCharCode("0x20bb7"));//
29     console.log(String.fromCodePoint("0x20bb7"));//𠮷
30 }
31 {
32     let str='\u{20bb7}abc';
33     for(let i=0;i<str.length;i++){
34         console.log('es5',str[i]);
35         //es5 �
36         //es5 �
37         //es5 a
38         //es5 b
39         //es5 c
40     }
41     for(let code of str){
42         console.log('es6',code)
43         //es6 𠮷
44         //es6 a
45         //es6 b
46         //es6 c
47     }
48 }
49 {
50     let str="string";
51     //includes,判断是否包含字符串('x')
52     console.log('includes',str.includes('r'));
53     console.log('includes',str.includes('c'));
54     //startsWidth,判断是否('xx')字符串起始
55     console.log('start',str.startsWith('str'));
56     //endsWith,判断是否('xx')字符串结尾
57     console.log('end',str.endsWith('str'));
58 }
59 {
60     let str="abc";
61     //字符串的复制(x),x次数
62     console.log(str.repeat(2));//abcabc
63 }
64 //字符串模板内容,拼接
65 {
66     let name="list";
67     let info="hello world";
68     let m=`i am ${name},${info}`;
69     console.log(m);//i am list,hello world
70 }
71 
72 {    
73     //向前补,一共两位,(位数,补的字符)
74     console.log('1'.padStart(2,'0'));//01
75     //向后补,一共两位,(位数,补的字符)
76     console.log('1'.padEnd(2,'0'));//10
77 }
78 //标签模板
79     //1.过滤字符串的时候,防止攻击的时候
80     //2.多语言转换
81 {
82     let user={
83         name:'list',
84         info:'hello world'
85     };
86     console.log(abc`i am ${user.name},${user.info}`);
87     function abc(s,v1,v2){
88         console.log(s,v1,v2);
89         return s+v1+v2;
90     }
91 }
92 
93 {    
94     //String.raw可以不转义\n
95     console.log(String.raw`Hi\n${1+2}`);//Hi\n3
96     console.log(`Hi\n${1+2}`);
97     //Hi
98     //3
99 }

 

posted on 2018-06-21 15:08  chenlw101  阅读(105)  评论(0编辑  收藏  举报