JavaScript substr () string intercept function using detailed

Source: Internet
Author: User

SUBSTR Definition and usage
The substr () method extracts a specified number of characters from the start subscript in a string.

Grammar
Stringobject.substr (Start,length)

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 demonstrates the use of the Substr method.

1. Intercepts the first character of the string.

<script type= "Text/javascript" >
var str= "jb51";
Alert (STR.SUBSTR (0,1));
</script>

The input result is J

2. Intercept the string starting with the 12th character, 5 lengths

<script type= "Text/javascript" >
function Substrdemo () {
var s, SS; Declares a variable.
var s = "The rain in Spain falls mainly in the plain.";
SS = S.substr (12, 5); Gets a substring.
return (SS); Returns "Spain".
}
Alert (Substrdemo ());
</script>

Input result is Spain

3. Use substr () to extract some characters from the string:

1)

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

The input result is Llo world!

2)

<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.substr (3,7))
</script>

The input result is lo worl

JavaScript substr () string intercept function using detailed

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.