Slice, Substring, and Substr functions of the JavaScript string are described and compared.

Source: Internet
Author: User

In JavaScript, Substring extraction is mainly implemented through one of the three methods: Slice, Substring, and Substr.
Copy codeThe Code is 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 and an optional end index (or length) parameter in the three methods.

But they differ in some important aspects:
1. The substr () method extracts a specified number of characters from a specified position.
Param: start starts to extract the location index of the character, and length is used to extract the length of the number of characters.
Return: A New String. The length starting from start.
Different browsers are inconsistent. modern browsers allow the start index parameter to be a negative number to indicate the number of characters to be extracted from the end of a string. However, in ie8 and earlier versions, the start index parameter is calculated at least from 0. [Substr is the ECMAScript feature appended to the Web browser. It is not recommended that the start index be negative when used]
Copy codeThe Code 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 earlier
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 from one index of a string to another, or until the end of the string.
Param: indexA, indexB two parameter values are an integer between 0 and the length of the string.
Return: returns a new string that ranges from the index to the index, including small index position characters, not large index position characters.
The substring parameter can be reversed. It always uses a small parameter value as the start and a large parameter value as the end. If the parameter is less than 0 or NaN, it is regarded as 0. If the parameter is greater than the length of the string, it is considered as the length value of the string.
Copy codeThe Code is 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 "Kill ill"
Console. log (anyString. substring (0, 6 ));

// Displays "Mozilla"
Console. log (anyString. substring (0, 7 ));
Console. log (anyString. substring (0, 10 ));

3. slice extracts part of the string.
Param: beginSlice starts to extract the index of the character location, which can be negative. If it is negative, it is regarded as (sourceLength-beginSlice), and sourceLength is the length of the string, that is: index of the position of the extracted characters starting from the end of the string. If omitted, the extraction ends. If it is a negative value, it is regarded as (sourceLength-endSlice ).
Return: returns a new string of all characters starting from start to end (excluding end.
All parameters can be negative. If the index is negative, it starts from the end of the string.
Copy codeThe Code 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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.