In JavaScript, extracting substrings mainly uses one of the three methods: Slice, Substring, and Substr.
Copy the code as follows:// slice
// Syntax: string.slice(start [, stop])
"Good news, everyone!".slice(5,9);
// 'news'
// substring
// Syntax: string.substring(indexA [, indexB])
"Good news, everyone!".substring(5,9);
// 'news'
// substr
// Syntax: string.substr(start [, length])
"Good news, everyone!".substr(5,4);
// 'news'
Enter a start index parameter among the three methods and an optional end index (or length) parameter.
But they differ in some important aspects:
1. The substr() method extracts the specified number of characters from the specified position.
param: start start extracting the position index of the characters, length extracting the number and length of the characters.
return: A new string. length characters starting from start.
There are inconsistent manifestations in different browsers. Modern browsers allow the start index parameter to be negative to represent the number of characters extracted from the end of the string. However, in ie8 and below browser start index parameters are calculated from 0 at least. 【Substr is an ECMAScript feature attached to the web browser, and it is not recommended to use the start index as a negative value】
The code copy is as follows: var str = "abcdefghij";
console.log("(1): " + str.substr(1)); // (1): bcdefghij
console.log("(1,2): " + str.substr(1,2)); // (1,2): bc
console.log("(-3): " + str.substr(-3)); // (-3): hij
console.log("(-3,2): " + str.substr(-3,2)); // (-3,2): hi
console.log("(20, 2): " + str.substr(20, 2)); // (20, 2):
console.log("(-20, 2): " + str.substr(-20, 2)); // (-20, 2): ab
// ie8 and below
console.log("(-3): " + str.substr(-2)); // (-20, 2): hij
console.log("(-3, 2): " + str.substr(-2)); // (-20, 2): ab
2. The substring() method is used to extract a subset of a string index index to another string, or until the end of the string.
param: indexA, indexB The two parameters take values ranging from a range of 0 to an integer between the length of the string.
return: Return a new string, starting from the small index to the large index, including small index position characters, not large index position characters.
The parameters of substring are invertible, and it always starts with small parameter values and large parameter values as ends. If the parameter is less than 0 or NaN, it is considered 0, and if the parameter is greater than the length of the string, it is considered as the length value of the string.
Copy the code as follows://assumes a print function is defined
var anyString = "Mozilla";
// Displays "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
// Displays "lla"
console.log(anyString.substring(4,7));
console.log(anyString.substring(7,4));
// Displays "Mozill"
console.log(anyString.substring(0,6));
// Displays "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(0,10));
3.slice Extract part of the string.
param: beginSlice starts extracting the position index of the character, which can be negative. If it is a negative value, it is considered (sourceLength-beginSlice), sourceLength is the length of the string, that is, the position calculated from the endSlice ends the extracted The position index of the character. If omitted, the extraction is ended. If it is a negative value, it is considered (sourceLength-endSlice).
return: Returns a new string, all characters from start (including start) to end (excluding end).
All parameters can be negative. If the index is negative, it starts from the end of the string.
The code copy is as follows:
var str1 = "The morning is upon us.";
console.log(str1.slice(4, -2)); // morning is upon u
var str = "The morning is upon us.";
str.slice(-3); // "us."
str.slice(-3, -1); // "us"
str.slice(0, -1); // "The morning is upon us"