There are two major substring and substr functions in javascript For string truncation. Next I will introduce the usage of the two string interceptions respectively. If you need to know about them, refer.
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.
Only one parameter
| The Code is as follows: |
Copy code |
|
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to the qiongtai blog ';
Var sub = str. substring (3 );
Alert (sub); // out: Visit the qiongtai blog
</Script> |
Two Parameters
| The Code is as follows: |
Copy code |
|
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to the qiongtai blog ';
Var sub = str. substring (3,11 );
Alert (sub); // out: Enter the qiongtai blog
</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.
Only one parameter
| The Code is as follows: |
Copy code |
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to the qiongtai blog ';
Var sub = str. substr (3 );
Alert (sub); // out: Visit the qiongtai blog
</Script>
|
Two Parameters
| The Code is as follows: |
Copy code |
|
<Meta charset = "UTF-8"/>
<Script type = 'text/javascript '>
/**
* Substring function DEMO
*/
Var str = 'Welcome to the qiongtai blog ';
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.
Other functions
Function: split ()
Function: uses a specified separator to split a string and store it in an array.
Example:
| The Code is as follows: |
Copy code |
Str = "jpg | bmp | gif | ico | png ";
Arr = theString. split ("| ");
// Arr is an array containing character values "jpg", "bmp", "gif", "ico", and "png" |
Function: John ()
Function: combines an array into a string using the separator you selected.
Example:
| The Code is as follows: |
Copy code |
Var delimitedString = myArray. join (delimiter );
Var myList = new Array ("jpg", "bmp", "gif", "ico", "png ");
Var portableList = myList. join ("| ");
|
// The result is jpg | bmp | gif | ico | png.
Function: indexOf ()
Function: returns the subscript of the first character matching the substring in a string.
| The Code is as follows: |
Copy code |
|
Var myString = "JavaScript ";
Var w = myString. indexOf ("v"); w will be 2
Var x = myString. indexOf ("S"); x will be 4
Var y = myString. indexOf ("Script"); y will also be 4
Var z = myString. indexOf ("key"); z will be-1 |
Example
Example:
| The Code is as follows: |
Copy code |
<Script type = "text/javascript">
Var str = "0123456789 ";//
Alert (str. substring (0); // ------------ "0123456789"
Alert (str. substring (5); // ------------ "56789"
Alert (str. substring (10 ));//-----------""
Alert (str. substring (12 ));//-----------""
Alert (str. substring (-5); // ----------- "0123456789"
Alert (str. substring (-10); // ---------- "0123456789"
Alert (str. substring (-12); // ---------- "0123456789"
Alert (str. substring (0, 5); // ---------- "01234"
Alert (str. substring (0, 10); // --------- "0123456789"
Alert (str. substring (0, 12); // --------- "0123456789"
Alert (str. substring (2, 0); // ---------- "01"
Alert (str. substring (2, 2 ));//----------""
Alert (str. substring (2, 5); // ---------- "234"
Alert (str. substring (2,12); // --------- "23456789"
Alert (str. substring (2,-2); // --------- "01"
Alert (str. substring (-1, 5); // --------- "01234"
Alert (str. substring (-1,-5 ));//--------""
Alert (str. substr (0); // --------------- "0123456789"
Alert (str. substr (5); // --------------- "56789"
Alert (str. substr (10 ));//--------------""
Alert (str. substr (12 ));//--------------""
Alert (str. substr (-5); // -------------- "0123456789"
Alert (str. substr (-10); // ------------- "0123456789"
Alert (str. substr (-12); // ------------- "0123456789"
Alert (str. substr (0, 5); // ------------- "01234"
Alert (str. substr (0, 10); // ------------ "0123456789"
Alert (str. substr (0, 12); // ------------ "0123456789"
Alert (str. substr (2, 0 ));//-------------""
Alert (str. substr (2, 2); // ------------- "23"
Alert (str. substr (2, 5); // ------------- "23456"
Alert (str. substr (2, 12); // ------------ "23456789"
Alert (str. substr (2,-2 ));//------------""
Alert (str. substr (-1, 5); // ------------ "01234"
Alert (str. substr (-1,-5 ));//-----------""
</Script>
|