JavaScript intercepts the Slice Substring and Substr functions of strings

  • 2020-03-30 02:25:08
  • OfStack

In JavaScript, substrings are extracted mainly through Slice, Substring, and Substr.

// 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 one of the three methods   The index parameter for start, and an optional end index (or length) parameter.

But they differ in important ways:
1. The substr() method extracts the specified number of characters from the specified location.
Param: start starts to extract the position index of the character, and length extracts the number length of the character.
Return: a new string. Length of characters starting at start.
Inconsistent performance across browsers, modern browsers allow the start index parameter to be negative to indicate the number of characters to be extracted starting at the end of the string. But in ie8 and below version of the browser start index parameter minimum from 0. Substr is an additional ECMAScript feature for Web browsers. Negative start index is not recommended.

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
//Internet explorer and the following
console.log("(-3): " + str.substr(-2)); // (-20, 2): hij
console.log("(-3, 2): " + str.substr(-2)); // (-20, 2): ab

The substring() method is used to extract a subset of a string from one index index to another, or to the end of the string.
Param: indexA, indexB two parameters range from 0 to the length of the string integer.
Return: returns a new string from a small index to a large index, including small index position characters, not large index position characters.
The parameters of a substring are reversible, and it always starts with a small parameter value and ends with a large parameter value. If the parameter is less than 0 or NaN, it is treated as 0, and if the parameter is greater than the length of the string, it is treated as the length value of the string.
// assumes a print function is defined
var anyString = "Mozilla";
// Displays "Moz"
// Displays "lla"
// Displays "Mozill"
// Displays "Mozilla"

Slice extracts part of a string.
Param: begin with beginSlice to extract the position index of the character, which can be negative, if negative is considered (sourcelength-beginslice), the sourceLength is the length of the string, that is, the position index of the extracted character at the end of endSlice from the end of the string. If omitted, extract to the end. If it is negative, it is considered (sourcelength-endslice).
Return: returns a new string, all characters from start (including start) to end (excluding end).
All arguments can be negative, and if the index is negative, it starts at the end of the string.

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"

Related articles: