ES6的字符串

ES6 之前判断字符串是否包含子串,用 indexOf 方法,ES6 新增了子串的识别方法。
1.includes(): 返回布尔值,判断是否找到参数字符串。
2.startWith():返回布尔值,判断参数字符串是否在原字符串的头部。
3.endsWith():返回布尔值,判断参数字符串是否在原字符串的尾部。

字符串扩展

Unicode和UTF-16是什么和他们的关系
用for-of遍历字符

模板字符串

模板字符串相当于加强版的字符串,用反引号 `,除了作为普通字符串,还可以用来定义多行字符串,还可以在字符串中加入变量和表达式。
用法:

//普通字符串
let string = `Hello'\n'world`;
console.log(string); 
   // "Hello'
   // 'world"
//多行字符串:
let string1 =  `Hey,
can you stop angry now?`;
console.log(string1);
  // Hey,
  // can you stop angry now?

举个栗子

//模板字符串
// 模版字符串
// 'asdffdsa'
// "asdfasdf"

// `asdfasdffsa`

 const xiaoming = {
    name: '小明',
   age: 14,
    say1: function() {
        console.log('我叫' + this.name + ', 我今年' + this.age + '岁!');
    },
    say2: function() {
        console.log(`我叫${ { this.name}, 我今年${ this.age }岁!`);
    }
}

xiaoming.say1();
xiaoming.say2();

显示:


1.png

字符串的扩展提供部分新方法

padSatrt padEnd

{
  let str ='i';

 let str1=str.padStart(5,'mooc');
console.log(str1);

let str2=str.padEnd(5.'mooc');
console.log(str2);
}

输出:


1 .png

repeat

{
  console.log('i'.repeat(10)); 
 function repeat (str,num){
 return new Array(num +1).join(str);
}
console.log(repeat('s',3));
}

输出:


1.png

startsWith endWith

{
const str ='A promise is a promsie';

console.log(str.startsWith('B'));
console.log(str.startWith('A pro'));

console.log(str.endsWith('promsie'));
console.log(str.endsWith('A'));
}

输出:


1.png

includes

{
 const str ='A promise is a promsie';
if(str.indexOf('promise')!==-1){
  console.log('存在');
 }
}

输出:


1.png

padStart() 与padEnd()都是可以实现字符串补全长度的功能。

for-of遍历字符串

Unicode表示法

Unicode是一项标准 包括字符集、编码方案等
他是为了解决传统的字符编码方案的局限而产生的,为每种语言中的每个字符设定了统一并且唯一的二进制编码。以满足跨语言、跨平台进行文本转换、处理的要求

你可能感兴趣的:(ES6的字符串)