字符串

 1 <script>
 2     'use strict'//严格模式,没用关键字申明的变量都会报错
 3     // 字符串
 4     // 用''或""括起来的字符表示。
 5     /* 
 6         转义字符\
 7         转义字符\可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\。
 8     */
 9     console.log('I\'m \"ok"\!') //I'm "ok"!
10 
11     /*
12         多行字符串
13         e6用反引号`...`
14     */
15     var str = `我
16                 是一个
17                 多行
18                 字符串`
19     console.log(str)
20 
21     /*
22         模板字符串
23         要把多个字符串连接起来,用+号连接 (缺点:变量多的情况下比较麻烦)
24     */
25     var name = '小家伙'
26     var age = 3;
27     console.log('我是' + name + ',我今年' + age + '岁了')
28     //用E6中的多行字符串和${}方法比较简洁
29     console.log(`我是${name},我今年${age}岁了`)
30 
31     /*
32         操作字符串
33 
34         .length 获取字符串长度
35         [] 用下标值获取指定位置的字符,索引从0开始 
36             注:字符串是不可变的,如果对字符串的某个索引赋值,不会有任何错误,但是,也没有任何效果
37         toUpperCase() 把字符串变为大写
38         toLowerCase() 把字符串变为小写
39         indexOf() 搜索指定字符串出现的位置
40         substring() 返回指定索引区间的字符串
41     */
42     var str2 = 'Hello World!'
43     console.log(str2.length)//12
44     console.log(str2[3]) //l
45     console.log(str2[12])// undefined 超出范围的索引不会报错,但一律返回undefined
46     console.log(str2.toUpperCase())//HELLO WORLD!
47     console.log(str2.toLowerCase())//hello world!
48     console.log(str2.indexOf('World'))//6  没找到会返回-1
49     console.log(str2.substring(0,5))//Hello
50 </script>

 

posted @ 2017-07-31 08:58  咬尾巴的狐狸  阅读(263)  评论(0编辑  收藏  举报