ES6 extensions to strings

Refer to Ruan Yifeng ES6

find substring

includes , startWith

Both methods support a second parameter n, which means to search for a substring from the beginning of the current 第n个位置string; the default is 0.

  • includes(): Returns a boolean value indicating whether the parameter string was found
  • startWith(): Returns a boolean value indicating whether the parameter string is at the head of the current string

    endWith

    endsWith(str,n): Returns a boolean value, indicating whether the string stris at the end of the current string, nindicating that the search range is the current string 前n个字符, the ndefault is the current stringlength

    repeat

    repeat(n)method that repeats the current string ntimes and returns a新字符串
  • If n is a string, it will be automatically converted to a number
  • If n is a decimal, it will be automatically rounded
  • n cannot be a 负数(-1~0之间的小数除外)sum Infinity, otherwise an error will be reported
  • n NaNis equivalent to0

    pathStart , pathEnd

    padStart(len,str), padEnd(len,str), these two methods are used to complete the string, lenspecifying the length of the completed string; strit is the string used to complete, the default is a space
  • If the length of the original string is greater than or equal lento, return the original string
  • If the sum of the original string and the strlength is greater than lenthe length, truncate strthe extra part

    template string

    template string, 反引号``used to identify

Use as normal string

console.log(typeof `haha`); // string

Used to define multi-line strings

Whitespace and indentation are preserved when outputting

let w = `haha
  hehe
      heiehi`;
console.log(w);
/* 
haha
  hehe
      heiehi */

backticks to be escaped

Use backticks in template strings \to escape them with a backslash, for example:`\`hello\` World!`

Embedded variables

The variable name should be written in ${}, {}and can be placed in any js表达式, can be operated, can refer to object properties, and can call functions

  • {}The variables in must be declared, otherwise an error will be reported
  • {}If the value in is not a string, it will be converted to a string according to the general rules

    let x = 1;
    let y = 2;
    let obj = { a: 3, b: 4 };
    let arr = [1, 'hi'];
    let fn = function () {
      console.log('hello');
    }
    console.log(`${x}` + `${y}`);       // 12
    console.log(`${obj.a + 1 + obj.b}`);// 8
    console.log(`${arr}`);  // 1,hi
    `${fn()}` // hello

    nested

    ``javascript const tmpl = addrs =>


    ${addrs.map(addr => <tr><td>${addr.first}</td></tr> <tr><td>${addr.last}</td></tr>).join('')}

    `;
    const data = [
    { first: '

Bond Lars

*/
### 标签模板 即`` fn`${}` ``的形式,`fn`是一个函数,`fn`被调用来处理后面的模板字符串 - 函数`fn`的第一个参数是一个数组,包含模板字符串中没有被变量替换的部分,其他参数是模板字符串中各个被变量替换后的值javascript
let a = 5;
let b = 'hello';
fnze; // [["ze"]]
fnone${a}two${b}${'three'}; // [["one", "two", "", ""], 5, "hello", "three"]
function fn(arr) { console.log(arguments); }
```

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306190&siteId=291194637