The slice, Substring, substr functions of JavaScript intercepting strings and comparing _javascript techniques

Source: Internet
Author: User

In JavaScript, extracting substrings is mainly done through Slice, Substring, Substr, one of three methods.

Copy Code 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 an index parameter for start in three methods, and an optional end index (or length) parameter.

But they differ in a number of important ways:
The 1.substr () method extracts the specified number of characters from the specified position.
Param:start begins to extract the position index of the character, length to extract the number of characters.
Return: A new string. The length character starting at start.
Inconsistent performance in different browsers, modern browsers allow the start index parameter to be negative, to represent the beginning of the string at the end, the number of characters extracted. However, the IE8 and the following versions of the browser start index parameters are calculated at the minimum starting from 0. "Substr the ECMAScript attribute attached to the Web browser and does not recommend that the start index be negative when used"

Copy Code code 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 ("(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


The 2.substring () method is used to extract a subset of the string from one index index to another, or until the end of the string.
Param:indexa, Indexb Two arguments take a value range of integers between 0 and the length of the string.
Return: Returns a new string, starting from small index to large index, including smaller index position characters, excluding large index position characters.
The substring argument is reversible, always using a small parameter value as the start, and a large parameter value as the end. If the argument is less than 0 or Nan, it is considered to be 0, and if the argument is greater than the length of the string, it is considered the length value of the string.
Copy Code 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 extracts part of the string.
Param:beginslice begins to extract the position index of the character, can be negative, if the negative value is considered (Sourcelength-beginslice), Sourcelength is the length of the string, That is, the position index of the character to be endslice at the end of the string. If omitted, extract to end. If a negative value is treated as (sourcelength-endslice).
Return: Returns a new string of all characters starting at start (including start) and ending with end (excluding ends).
Arguments can be negative, and if the index is negative, start at the end of the string.

Copy Code code 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"

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: info-contact@alibabacloud.com 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.