Summary of String interception Substring/substr/slice methods in JavaScript

Source: Internet
Author: User
Tags truncated

Recently the whole front desk always more or less encounter some small problem of string interception, can't help looking carefully under. Let's take a look at some of the application experiences and differences between string interception methods in JavaScript. Read the other bloggers have also written, each has a style and just a personal with the mind, their own experience is the most profound. Some seemingly simple and great article things to understand you know that you learn very shallow, part of the content is W3cschool original, and then add their own summary and insights. You can also go to the official website to see what you are interested in.

The following is a summary:

var str = "0123456789"; This string is used for context examples

Stringobject.substring (start,stop): Extracts the characters between two specified index numbers in a string. (W3school definition)

1. Intercept the characters between the index start and stop, and the character length is (Stop-start).

2. The index starts at the left of the string 0, and the string includes [Start,stop], which includes start without stop.

Example: var ss = str.substring (1,5);

SS 1234 includes a string with an index of 1, and does not include a character channeling with an index of 5 position. String length is 5-1=4

3. If the parameter start is equal to stop, then the method returns an empty string.

4. If start is larger than stop, the method swaps the two parameters before extracting the string.

Example: Str.substring (5, 2) = Str.substring (2, 5)

5. If start or stop has a negative number, the negative number is first converted to 0, then the 4th is executed, and the string intercept is executed.

Example: str.substring (3,-5) →str.substring (3, 0) →str.substring (0, 3) //result is 012

6. The parameter stop can be omitted, omit the case from start intercept to the end of the string.

Example: Str.substring (5); Result is 56789

If you try it yourself, you'll find that substring is just a string that intercepts between two indexes, and a negative number becomes 0 directly. It seems that the order can be ignored directly, even if Start > stop he will sort, as to whether this will affect the efficiency of implementation, I did not delve into, bored when interested can be studied.

Stringobject.slice (Start,end): Method extracts a portion of a string and returns the extracted part with a new string. (W3school definition)

W3school definition I only understand the first half, the slice () method can not only manipulate the string object, can also manipulate the Arrayobject is the array object, based on the second we only explain the string interception, the array is skipped (attached to the official website link).

1. Intercept the characters between the index start and stop, and the character length is (Stop-start). [Effect same as substring]

2. The index starts at 0, and the string includes [Start,stop], which includes start without stop. [Effect same as substring]

3. If the parameter start is equal to stop, then the method returns an empty string. [Effect same as substring]

4. The parameter stop can be omitted, omit the case from start intercept to the end of the string. [Effect same as substring]

5. If start is larger than stop, sorting is not done here, and the result of the interception is "" empty string.

Example: Str.slice (5, 1); The result is ""

6. If start is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.

Example: str.substring ( -2); //result is 89 (9 index is-1 8 index is-2, stop ellipsis is truncated to last, starting from 8 to last)

7. If stop is a negative number, it specifies the position from the end of the string, with 6th.

Example: Str.substring ( -5,-4); Result is 5

8. The following example:

Str.substring (5, 8); The result is "567"

Str.substring (5, 3); The result is ""

Str.substring ( -5,8); The result is "567"

Str.substring ( -5,3); The result is ""

Str.substring ( -5,-3); The result is "56"

Because it involves looking at the index from the end of the string when a negative number is involved, it makes the whole look a bit complicated, so I prefer to use the location of the index to explain it. Slice () looks like a way to catch the index position rensili, I just intercept the part between the start position and the stop position, and if the stop position is an empty string before start, it will not be intercepted until the start position. So we can solve the slice () method with a number of cases, positive numbers from the front number, negative numbers from the end of the number, in the case of the first stop before the start, take [Start, stop) of the part of the can.

Stringobject.substr (start,length): Extracts a specified number of characters from the start subscript in a string. (W3school definition)

In front of the two different places is that he is not getting a string between two-word indexes, such as definition, which is a string that intercepts length from an index.

The 1.start parameter is the index position at which the intercept begins, positive numbers start at the left 0, and negative numbers start at the right-1. Truncate the length string to the right.

Example: Str.substr (2, 5); The result is 23456 length of 5

2. If the parameter length is omitted, the end of the string is truncated from the index start.

3. When the parameter length is negative, it is converted to 0, so when the length is negative or 0 o'clock, the result of the interception is "" empty string.

website tips:

Important: ECMAscript does not standardize the method and therefore opposes its use.

Important: In IE 4, the value of the parameter start is invalid. In this BUG, start specifies the position of the No. 0 character. In a later release, this bug has been fixed.

Finally, there are several points of personal friendship tips:

1.substring case, if stop is longer than the string length, the run-in stop is the string length, then the string is truncated to the end.

Example: str.substring (-3, 20); //actual effect equivalent to str.substring (0, 10) or str.substring (0)

When the index of 2.slice exceeds the string length.

Example: Str.slice (-20, 20); //equals Str.slice ( -10,10) or Str.slice (0,10) or Str.slice (-10) or Str.slice (0)

Str.slice (-5, 20); equals Str.slice ( -5,10) or Str.slice (-5)

3.SUBSTR case.

Example: Str.substr (-15, 5); equals Str.substr (0,5);

Other cases are based on the above method of conversion, less than the minimum time to take the smallest, greater than the maximum when the maximum. Make it clear that the basics are up to the occasion.

I see on the Network substr () method is always written (not recommended), the specific reasons I am not clear, but still because of people, in short, make clear, choose their favorite is good. I just think it took a while to look at the way it used to be vague, to make myself clearer, and hopefully someone interested in answering my questions. The above are personal finishing information, there is a way to clear the point below the link into the official website.

W3cschool official website Address: http://www.w3school.com.cn/index.html JavaScript action string Object URL: http://www.w3school.com.cn/jsref/jsref_ Obj_string.asp Slice () method operation array: http://www.w3school.com.cn/jsref/jsref_slice_array.asp

Summary of String interception Substring/substr/slice methods in JavaScript

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: 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.