Differences between substr and substring in js

Source: Internet
Author: User

Differences between substr and substring in js

Someone asked the two differences in the group today. I will take this opportunity to list them here.

Substring (from,)

Start and end locations, index starting from scratch

Parameter description
From is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.

The substring returned by the substring method includes the characters at start, but not the characters at.
If from is equal to, this method returns an empty string (a string with a length of 0 ).
If the from parameter is greater than the to parameter, the two parameters are exchanged before the sub-string is extracted.
If from or to is a negative number, it is replaced with 0.

 

Substr (from, length)

Start is required. The starting position of the required substring. The index of the first character in the string is 0.
Length is optional. The number of characters to be included in the returned substring.

 

var str = 0123456789;alert(str.substring(0));------------0123456789alert(str.substring(5));------------56789alert(str.substring(10));-----------alert(str.substring(12));-----------alert(str.substring(-5));-----------0123456789alert(str.substring(-10));----------0123456789alert(str.substring(-12));----------0123456789alert(str.substring(0,5));----------01234alert(str.substring(0,10));---------0123456789alert(str.substring(0,12));---------0123456789alert(str.substring(2,0));----------01alert(str.substring(2,2));----------alert(str.substring(2,5));----------234alert(str.substring(2,12));---------23456789alert(str.substring(2,-2));---------01alert(str.substring(-1,5));---------01234alert(str.substring(-1,-5));--------alert(str.substr(0));---------------0123456789alert(str.substr(5));---------------56789alert(str.substr(10));--------------alert(str.substr(12));--------------alert(str.substr(-5));--------------56789alert(str.substr(-10));-------------0123456789alert(str.substr(-12));-------------0123456789alert(str.substr(0,5));-------------01234alert(str.substr(0,10));------------0123456789alert(str.substr(0,12));------------0123456789alert(str.substr(2,0));-------------alert(str.substr(2,2));-------------23alert(str.substr(2,5));-------------23456alert(str.substr(2,12));------------23456789alert(str.substr(2,-2));------------alert(str.substr(-1,5));------------9alert(str.substr(-1,-5));-----------    


 

 

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.