Differences between substr and substring

Source: Internet
Author: User
Tags truncated

Preface:

Heaven, earth, look, what is this?

Frozen chicken, blog is still being watched ~, Haha, continue to fuel ~

Tell us the difference between substr and substring in JS today

1. Definition of w3school

1) substr

Definition: The substr () method can be extracted from stringsStartA specified number of characters starting with a subscript.

Usage:Stringobject. Substr (Start,Length)

Start Required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.
Length Optional. The number of characters in the substring. It must be a numerical value. If this parameter is omittedStringobjectThe start position to the end of the string.

Case:

① Substr only transmits One Parameter

 1 

Output:

 

The string is intercepted starting from 3rd bits.

② Substr Two Parameters

 1 

Output:

String truncation starts from 3rd bits and starts to count 7

 

2) substring

Definition: The substring () method is used to extract characters of a string between two specified subscripts.

Usage:Stringobject. Substring (Start,Stop)

   
Start Required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Stop

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.

Case:

① Substring only transmits One Parameter

 1 

Output:

 

The string is intercepted starting from 3rd bits.

② Substring Two Parameters

 1 

Output:

 

The string is truncated starting from 3rd bits and the coordinate is 7 characters.

 

2. Comparison

Similarities: both functions are the same when passing a parameter.

Differences:

Substr: the position where the negative number starts from the end of the string.

Two parameters, the last one being several lengths from the truncated position, Length

Substring: two parameters. The last one is from the captured position to the coordinate, stop

One length and one stop

This is easy to understand, hahaha,

Finally, I wish you a happy 1024 day. I would like to celebrate overtime ~~

 

 

Differences between substr and substring

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.