String Object slice and substring differences in JavaScript

Source: Internet
Author: User

The 1, Slice (start,stop), and substring (start,stop) methods are used to extract characters from start to stop-1 in a string (since the string index starts at 0). Where start must be selected, stop is optional.

2, now mainly talk about the differences between the two

  SUBSTRING (start,stop):

1. Returns an empty string when start equals stop.

2.stop Optional: If this argument is omitted, the returned substring continues until the end of the string.

3. If the start>stop,substring swaps two parameters, the end of the start-1 position starts at the beginning of the end parameter.

4. If any value in start or stop is greater than the length of the string, the value will be replaced by the length of the string. After the replacement to see if the condition 3 is met, then go on to the 3rd step.

5. If any value in start and stop is negative (<0) or Nan (not a number), then this value is treated as zero.

  Slice (start,stop):

1. Returns an empty string when start equals stop, and substring is the same effect.

2.stop Optional: If this argument is omitted, the returned substring continues until the end of the string. The same effect as substring.

3. If the Start>stop,slice does not swap two parameter positions, it returns null "" directly.

4. If any value in start or stop is greater than the length of the string, the value will be replaced by the length of the string. The same effect as substring.

5. If start is a negative number (<0): Then the truncated string will start at the end of the stop with a string long datum, minus the start absolute value string.
Eg:var str = "abcdef"; var s1=str.slice ( -5,3); Console.info (S1)//BC ends at 6-5 to 3, 6 is string length =slice (1,3);

    If End is negative: Then the stop is replaced by (length-1)-math.abs (stop);

Eg:var str= "abcdef"; var s1=str.slice (1,-2); Console.info (S1); BCD ends at 1 to 6-1 -2=3

And finally there's a subStr (start.length)

This is a good distinction, he returns the length character starting at start (including the character that the start refers to), not specifying length or length to return to the end string.

 

      

    

String Object slice and substring differences 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.