美文网首页
ES6——字符串

ES6——字符串

作者: benbensheng | 来源:发表于2019-04-23 14:59 被阅读0次

1. 子串的识别

  • includes():返回布尔值,判断是否找到参数字符串。
  • startsWith():返回布尔值,判断参数字符串是否在原字符串的头部。
  • endsWith():返回布尔值,判断参数字符串是否在原字符串的尾部
let string = "apple,banana,orange";
string.includes("banana");     // true
string.startsWith("apple");    // true
string.endsWith("apple");      // false
string.startsWith("banana",6)  // true  可以设置一个起始位置

2. 字符串重复

  • repeat():返回新的字符串,表示将字符串重复指定次数返回
console.log("Hello,".repeat(2));  // "Hello,Hello,"

3. 字符串补全

  • padStart:返回新的字符串,表示用参数字符串从头部补全原字符串。
  • padEnd:返回新的字符串,表示用参数字符串从头部补全原字符串。
    以上两个方法接受两个参数,第一个参数是指定生成的字符串的最小长度,第二个参数是用来补全的字符串。如果没有指定第二个参数,默认用空格填充。
console.log("h".padStart(5,"o"));  // "ooooh"
console.log("h".padEnd(5,"o"));    // "hoooo"
console.log("h".padStart(5));      // "    h"
  • 如果指定的长度大于或者等于原字符串的长度,则返回原字符串:
console.log("hello".padStart(5,"A"));  // "hello"

*常用于补全位数:

console.log("123".padStart(10,"0"));  // "0000000123"

4. 模板字符串

  • 普通字符串
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?
  • 字符串插入变量和表达式。
let name="ben";
let age=20;
let info=`my name is ${name},i am ${age+3} years old`;
console.log(info)
  • 填写html
$('#list').html(`
<ul>
  <li>first</li>
  <li>second</li>
</ul>
`);

5. 标签模板



相关文章

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

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

  • ES6模版字符串

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

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

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

  • 字符串

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

  • es6新特性

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

  • es6 知识

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

  • 字符串

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

  • 字符串语法糖笔记

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

  • es6的模板字符串

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

  • 一些关于es6的学习

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

网友评论

      本文标题:ES6——字符串

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