[JavaScript] extension of ES6 string

character string

String processing in ES5

  • The string template output of ES5 usually uses + splicing
let name = "superman";
let age = 18;
let str = "full name:" + name + ";Age:" + age + "year";
console.log(str); // Name: superman; Age: 18

String template for ES6

  • Enclose with ` ` and you can wrap at will
  • When representing multiline strings, all spaces and indents are retained in the output
  • When embedding a variable in the template string, you need to write the variable name in ${}
  • Arbitrary JavaScript expressions can be placed inside braces
let name = "superman";
let age = 18;
let str = `
full name: ${name};
Age: ${age} year
`;
console.log(str);
// 
// Name: superman;
// Age: 18
// 
Template string as parameter
  • The template string will be processed into multiple parameters before calling the function
  • Processing logic: take ${} as the dividing point to form an array of separated strings as the first parameter; The remaining variables in ${} are subsequent parameters
let name = "superman";
let age = 18;

function output() {
    console.log(arguments);
}

output("" + name + "" + age + ""); // ["Name: superman; age: 18"...]
output `full name: ${ name };Age: ${ age } year`; // [Array(3), "superman", 18...]
output(['full name:', ';Age:', 'year'], name, age); // [Array(3), "superman", 18...]

New function

includes()*

Whether it contains a parameter string and returns a Boolean value

str.include(substr[, num]);

substr: query string; num: the subscript to start query; can be omitted; the default value is 0

let str = "apple banana";
console.log(str.indexOf("apple")); // 0

// Query from the position with subscript 3
console.log(str.includes("apple", 3)); // false

startsWith()

Whether to start with a parameter string and return a Boolean value

str.startsWith(substr[, num]);

substr: query string; num: the subscript to start query; can be omitted; the default value is 0

let str = "http://www.baidu.com";
str.startsWith("http"); // true
str.startsWith("https"); // false

// The second parameter is the subscript to start the query. Does it start with the parameter string at this position
str.startsWith("www", 7); // true

endsWith()

Whether to end with a parameter string and return a Boolean value

str.endsWith(substr[, num]);

substr: query string; num: whether the first few characters end in the parameter string

let str = "http://www.baidu.com";
str.endsWith("com"); // true
str.endsWith("cn"); // false

// Whether the first 16 characters of str end with 'baidu'
str.endsWith("baidu", 16); // true

repeat()

Repeat num times str, Num is the number of repetitions, cannot be < 0

str.repeat(num);
let str = "superman";
let reStr = str.repeat(2);
console.log(reStr); // supermansuperman
Parameter precautions:
  1. The parameter NaN is equal to 0, and repeat(0) returns the empty string ''
  2. If the parameter > 0 and is decimal, it will be rounded down
  3. If the parameter < = - 1 / = infinity, an error will be reported
  4. If the parameter is between 0 and - 1, it is equal to 0
  5. If the parameter is a string, it is implicitly converted to a number

padStart()

  • Add prefix and return the added string
  • Do not change the original string
str.padStart(num[, pStr]);

num: length after adding; pStr: added string

let str = "su";
let newStr = str.padStart(5, '666per');
console.log(newStr); // 666su
Parameter precautions:
  1. STR is returned if str.length > = num
  2. If str.length + pStr If length > num, the excess pStr will be truncated
  3. Default pStr = '' (space string)
  • We can add prefixes of any length through the length attribute:
let str = "su";
let padStr = "666per"; // The prefix can be changed at will
let newStr = str.padStart(str.length + padStr.length, padStr);
console.log(newStr); // 666persu

padEnd()

  • Add a suffix and return the added string
  • Do not change the original string
str.padEnd(num, pStr);

num: length after filling; pStr: string used for padding

let str = "super";
let padStr = "man";
let newStr = str.padEnd(str.length + padStr.length, padStr);
console.log(newStr); // superman

Keywords: Javascript ECMAScript

Added by thewabbit1 on Thu, 16 Dec 2021 07:36:40 +0200