Introduction to the two functions of the string intercepted by JavaScript.

Source: Internet
Author: User

Introduction to the two functions of the string intercepted by JavaScript.

First, let's take a look at the usage of the substring function.

I. substring

Substring requires at least one parameter. The first parameter is the starting position, and the second parameter is optional. It is the ending position.

There is only one parameter:
Copy codeThe Code is as follows:
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to your children's shoes for guests ';
Var sub = str. substring (3 );
Alert (sub); // out
</Script>

Two parameters:
Copy codeThe Code is as follows:
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to your children's shoes for guests ';
Var sub = str. substring (3,11 );
Alert (sub); // out: comes to the house of friends
</Script>

Ii. substr

Substr also requires at least one parameter. The first parameter is the starting position, and the second parameter is optional, which is the length.

There is only one parameter:

Copy codeThe Code is as follows:
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to your children's shoes for guests ';
Var sub = str. substr (3 );
Alert (sub); // out
</Script>

Two parameters:
Copy codeThe Code is as follows:
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to your children's shoes for guests ';
Var sub = str. substr (3, 2 );
Alert (sub); // out: a child
</Script>

As shown in the preceding example, the results of substring and substr are the same only when there is only one parameter.


Function for intercepting strings in javascript

Str1 = str. substring (start, end) // intercept
Index = str. indexOf (str1) // returns the position of str1 in str.

Javascript string Truncation

<Html>

<Body>
<Input type = "checkbox" value = "1"/> 1
<Input type = "checkbox" value = "3"/> 3
<Input type = "checkbox" value = "6"/> 6
<Script type = "text/javascript">
Var str = "1, 2, 3, 4, 5 ";

Function checkThem (str ){
Arr = str. split (",");

Inputs = document. getElementsByTagName ("input ");
For (I = 0; I <arr. length; I ++ ){
For (j = 0; j <inputs. length; j ++ ){
If (inputs [j]. type = "checkbox" & inputs [j]. value = arr [I]) {
Inputs [j]. checked = true;
}
}
}
}

CheckThem (str );
</Script>
</Body>
</Html>

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.