es6--6.字符串相关

1.字符模板

   在es5中,我们经常拼接字符串

现在有了更简便的方法 使用反单引号``

反单引号 优点1.可以换行

      2.可以写入变量

         把变量写入到${变量}中

例如:

let a=12;

let str=`a23${a}b123c`;

 

2.endsWith();

作用: 判断是不是以什么结尾

例子:

 

let a='1.png';

if (a.endsWith('.gif')){
      alert('gif');      
} else if(a.endsWith('.png')){
      alert('png');  
}

 

3.startsWith()

作用: 判断是不是以什么开始

 

let a='1231.png';

if (a.startsWith('1')){
      alert('1');      
} else if(a.startsWith('2')){
      alert('2');  
}

 

posted @ 2017-12-13 16:07  魔都叛徒  阅读(135)  评论(0编辑  收藏  举报