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>