3.2.1 ES6 Strings
Category ES6 Tutorial
Extended Methods
Substring Identification
Before ES6, to determine if a string contains a substring, the indexOf method was used. ES6 introduces new methods for identifying substrings.
includes(): Returns a boolean value to determine if the argument string is found.
startsWith(): Returns a boolean value to determine if the argument string is at the beginning of the original string.
endsWith(): Returns a boolean value to determine if the argument string is at the end of the original string.
All three methods can accept two arguments: the string to be searched, and an optional search start position index.
let string = "apple,banana,orange";
string.includes("banana"); // true
string.startsWith("apple"); // true
string.endsWith("apple"); // false
string.startsWith("banana",6) // true
Notes:
These three methods only return boolean values. If the position of the substring is needed, indexOf and lastIndexOf must still be used.
If a regular expression is passed instead of a string to these three methods, an error will be thrown. indexOf and lastIndexOf, on the other hand, will convert the regular expression to a string and search for it.
String Repetition
repeat(): Returns a new string, indicating the string is repeated a specified number of times.
console.log("Hello,".repeat(2)); // "Hello,Hello,"
If the argument is a decimal, it is floored.
console.log("Hello,".repeat(3.2)); // "Hello,Hello,Hello,"
If the argument is a decimal between 0 and -1, it will be rounded to -0, which is equivalent to repeating zero times.
console.log("Hello,".repeat(-0.5)); // ""
If the argument is NaN, it is equivalent to repeating zero times.
console.log("Hello,".repeat(NaN)); // ""
If the argument is a negative number or Infinity, an error will be reported:
console.log("Hello,".repeat(-1));
// RangeError: Invalid count value
console.log("Hello,".repeat(Infinity));
// RangeError: Invalid count value
If a string is passed as an argument, it will first be converted to a number.
console.log("Hello,".repeat("hh")); // ""
console.log("Hello,".repeat("2")); // "Hello,Hello,"
String Completion
padStart: Returns a new string, indicating the original string is completed from the head (left side) with the argument string.
padEnd: Returns a new string, indicating the original string is completed from the tail (right side) with the argument string.
Both methods accept two arguments: the first is the specified minimum length of the generated string, and the second is the string used for completion. If the second argument is not specified, spaces are used by default.
console.log("h".padStart(5,"o")); // "ooooh"
console.log("h".padEnd(5,"o")); // "hoooo"
console.log("h".padStart(5)); // " h"
If the specified length is less than or equal to the length of the original string, the original string is returned.
console.log("hello".padStart(5,"A")); // "hello"
If the length of the original string plus the completion string exceeds the specified length, the excess completion string is truncated.
console.log("hello".padEnd(10,",world!")); // "hello,worl"
Commonly used for completing digits:
console.log("123".padStart(10,"0")); // "0000000123"
Template Strings
Template strings are like enhanced strings, using backticks `, in addition to being ordinary strings, they can also define multi-line strings and include variables and expressions within the strings.
Basic Usage
Ordinary strings
let string = `Hello'\n'world`;
console.log(string);
// "Hello'
// 'world"
Multi-line strings:
let string1 = `Hey,
can you stop angry now?`;
console.log(string1);
// Hey,
// can you stop angry now?
Insert variables and expressions into strings.
Variable names are written in ${}, and JavaScript expressions can be placed inside ${}.
```