JavaScript---function substring (position1,position2), Slice (Position1,position2), substr (position1,length)

Source: Internet
Author: User

Important: Unlike the slice () and substr () methods, substring () does not accept negative parameters.

The substring (Position1,position2) method is used to extract the character of a string intermediary between two specified subscripts. includes position1 characters, not including Position2 characters (position starting from 0).

Example one:

<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substring(3) ) </script>
Lo world!

Example two:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substring(3,7) ) </script>

Lo W

Important matters: The methods of the String object Slice (), substring (), and substr () (not recommended) can return the specified part of the string. Slice () is more flexible than substring () because it allows negative numbers to be used as arguments. Slice () differs from substr () because it specifies a substring with a position of two characters, and substr () specifies a substring with a character position and length.

The slice (position1,position2) method extracts a portion of a string and returns the extracted part with a new string. (spaces are also characters) include characters in the position1 position, excluding characters from the position2 position.

Example one:
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6)) </script>

Happy world!

Example two:
<script type= "Text/javascript" >var str= "Hello Happy world!" document.write (Str.slice (6,11)) </script>

Happy

Note:the substr () parameter specifies the starting position and length of the substring, so it can be used instead of substring () and slice ().

Important matters: ECMAscript did not standardize the method and therefore objected to its use.

Important: in IE 4, the value of the parameter start is invalid. In this BUG,start Specifies the position of the No. 0 character. In a later release, this bug has been fixed.

The substr (position1,length) method extracts a specified number of characters from a string, starting with the Position1 . length characters that include the position1 position character. 

Example one:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substr(3) ) </script>
Lo world!

Example two:
<script type= "Text/javascript" >var str= "Hello world!" document.write ( str.substr(3,7) ) </script>
Lo Worl

JavaScript---function substring (position1,position2), Slice (Position1,position2), substr (position1,length)

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.