美文网首页
ES6的字符串

ES6的字符串

作者: yezi1004 | 来源:发表于2018-12-01 23:18 被阅读0次

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

相关文章

  • 21.模板字符串和标签模板字符串

    ES6新增了模板字符串,用于字符串拼接 ES6新增了标签字符串 标签模板字符串执行结果: 函数的第一个参数为数组,...

  • ES6模版字符串

    初探ES6:字符串模板 && 标签模板 关键词:``,${} 字符串模板: 在ES6之前我们要在html或者con...

  • ES6-02 字符串与正则表达式

    ES6学习笔记-字符串与正则表达式 JS字符串编码 在ES6之前,JS的字符串以16位字符编码(UCS-2)为基础...

  • 字符串

    字符串换行使用 /n ,ES6可以使用反引号进行换行 ES6字符串模板使用 要获取字符串某个指定位置的字符,使用类...

  • 字符串

    1. 字符串方法。 2. ES6新增加的字符串方法。

  • es6新特性

    es6新特性 1.函数参数添加默认值 es6之前 es6之后: 2.字符串拼接 es6之前: es6之后: 3.解...

  • es6 知识

    字符串a.字符串拼接 //es5 var test = 'es6'; co...

  • es6的模板字符串

    关于es6的模板用法,想要拼接字符串使用传统的字符串拼接“+”是比较麻烦的,尤其拼接复杂的东西时,而es6为我们提...

  • 字符串语法糖笔记

    以前的字符串不能记录回车 es6 字符串可以作为参数传入函数

  • 一些关于es6的学习

    1.关于字符串的遍历 es6中字符串的遍历 var s="hdfghdsgfjhgfugjf" for(let ...

网友评论

      本文标题:ES6的字符串

      本文链接:https://www.haomeiwen.com/subject/adhdcqtx.html