JavaScript substr () string intercept function using the basics _

Source: Internet
Author: User
Tags numeric numeric value
SUBSTR Definitions and usage
The substr () method extracts a specified number of characters from the start subscript in a string.

Grammar
Stringobject.substr (Start,length)

Parameters Description
Start Necessary. The starting subscript of the substring to extract. Must be a numeric value. If it is a negative number, the argument declares the position from the tail of the string. In other words,-1 refers to the last character in the string, 2 to the penultimate character, and so on.
Length Optional. The number of characters in the substring. Must be a numeric value. If this argument is omitted, the string from the start position of the stringobject to the end is returned.

Description
If length is 0 or negative, an empty string is returned. If this argument is not specified, the substring continues to the end of the stringvar.

Example
The following example shows the use of the Substr method.

The first character of the first intercept string.

<script type= "Text/javascript" > var str= "jb51"; Alert (STR.SUBSTR (0,1)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The result is: j

<script type= "Text/javascript" > Function Substrdemo () {var s, SS;//DECLARE variable. var s = "The rain in Spain falls mainly in the plain."; SS = S.substr (12, 5); Gets the substring. return (SS); Returns "Spain". Alert (Substrdemo ()); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


It means to intercept the string, starting with the 12th character, 5 lengths.

In this case, we'll use substr () to extract some characters from the string:

<script type= "Text/javascript" > var str= "Hello world!" document.write (STR.SUBSTR (3)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Output:

Lo world!

Example 2

In this case, we'll use substr () to extract some characters from the string:

<script type= "Text/javascript" > var str= "Hello world!" document.write (Str.substr (3,7)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Output:

Lo Worl

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.