Difference between substr and substring in C #

Source: Internet
Author: User

These two methods are quite interesting, but the result is a 108,000 difference. I started from misuse of them and slowly entered their world. It is a coincidence that when start is 0, the two results are the same, and I have read the substr method before. Therefore, I had the illusion that only substr and no substring are available. When I find there are two ways, I will giggle at my ignorance. The following two methods are described:

Substr (start [, length]) indicates that length strings are obtained from the start position.
Substring (START, end) indicates a string from start to end, including the start position but not the end position.

 
The substring of JS and the substring of C # are used to extract a substring from a string, but their usage is quite different. Let's take a look at the following comparison:

Substring of JS

Syntax:

ProgramCode
String. substring (START, end)

Note:
Returns a substring from start to end (excluding end.

Example:

Program code
VaR STR = "abcdefgh ";
Document. Write (Str. substring (0, 1); // return:
Document. Write (Str. substring (2, 5); // return: CDE
Document. Write (Str. substring (7,8); // return: H

Substring of C #

Syntax:

Program code
String. substring (INT startindex)
String. substring (INT startindex, int length)

Note:
Returns a substring starting from startindex to ending, or a substring starting from startindex with a length of length.

Example:

Program code
String STR = "abcdefgh ";
Response. Write (Str. substring (0, 1); // return:
Response. Write (Str. substring (2, 3); // return: CDE
Response. Write (Str. substring (7,1); // return: H
Response. Write (Str. substring (7); // return: H
Response. Write (Str. substring (10); // error: startindex cannot exceed the string length.
Response. Write (Str. substring (7,10); // error: the index and length must reference the position in the string.

After the above instructions, we should have a clear understanding of their use, but there are several points to note about the substring of JS:

1. Start is not necessarily the first parameter, and end is not necessarily the second parameter. In substring (), the start position is 1, and the end position is 3;
2. when the substring to be returned is from the starting position to the end, the end value must be greater than or equal to the length of the string, such as the STR above. substring (), if the index starts from 0, the maximum value of end is 7, but 8 is used here. Of course, the same result is returned when the number is greater than 8, this is interesting.

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.