Compare JS's substring, substr, and C # substring

Source: Internet
Author: User

The role of JS Substring and C # substring is to intercept a substring from a string, but there are a lot of different ways to use them, so let's look at the following:
JS's substring
Grammar:

Program code string.substring (Start, end)

Description: Returns a substring from start to end that does not contain end.
Example:

Program code var str= "ABCDEFGH"; document.write (str.substring (0,1));//return:a document.write (str.substring (2,5));//return:cde document.write ( Str.substring (7,8));//return:h

substring in C #
Grammar:

Program code string.substring (int startIndex) string.substring (int startIndex, int length)

Description: Returns a substring starting from startindex to the end, or a substring that starts at startindex and length.
Example:

program code String str = "ABCDEFGH"; Response.Write (str. Substring (0,1));//return:a 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);//error:startindex cannot be greater than the string length. Response.Write (str. Substring (7,10));//error: The index and length must refer to the position within the string.

After the above instructions on their use should have a relatively clear understanding, but the JS substring still have a few points to explain:
1.start is not necessarily the first parameter, end is not necessarily the second parameter, substring (3,1), the starting position is 1, the end position is 3; 2. When the substring to be returned is from the start position to the end, The value of end must be greater than the length of the string (that is, the index greater than or equal to the string), such as the top of str.substring (7,8), according to the index from 0 to calculate the end of the maximum value is 7, but this is 8, of course, the use of more than 8 of the results returned is the same, this is more

JS's substr

Grammar:

Program code STRINGOBJECT.SUBSTR (start,length) Description: Extracts a specified number of characters from the start subscript in a string. Start Required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on. Length is optional. The number of characters in the substring. Must be numeric. If this argument is omitted, then the string from the beginning of the stringobject to the end is returned. Example: Program code
<script type= "Text/javascript" > var str= "Hello world!" document.write (STR.SUBSTR (3)) </script>

Output:

Lo world!

Compare JS's substring, substr, and C # 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.