First, substring and substr
|
Substring |
Substr |
|
Returns a substring of two indexes between (or to the end of a string) |
Returns the string starting at the specified position to a substring of the specified length |
syntax |
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201702/ 211606-20170207131932526-1612700630.png "style=" border:0px;margin-top:10px; "/> |
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201702/211606-20170207132303494-842242559. PNG "style=" border:0px;margin-top:10px; "/> |
Parameters |
Indexstart: An integer between 0 and the length of the string Indexend: Optional, an integer between 0 and the length of the string |
Start: The position at which to begin extracting characters, which can be negative Length: Optional. Number of characters extracted |
Describe |
1. Indexstart = Indexend, returns an empty string 2. Indexstart > Indexend, the effect is two parameters swapped the same as 3. Omit Indexend, extract the word selector until the end of the string 4. Either parameter < 0 or NaN, is treated as a 0 |
1. Start < 0, which is treated as Strlength + start (from the Count of strings) 2. Start < 0 && abs (start) > strlength , start = 0 3. Start >= strlength, Returns an empty string 4. Any parameter is nan and is treated as 0 5. Length <= 0, returns an empty string |
Code |
Online execution |
|
1) Substring description
Returns a substring between two indexes (or to the end of a string) of a string.
650) this.width=650, "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var anystring = "Mozilla";//String length is 7// output "Moz" Console.log (anystring.substring (0,3)); Console.log (anystring.substring (3,0));//indexstart > indexend, it is like two parameters to swap Console.log ( Anystring.substring (3,-3));//indexend < 0, then Indexend = 0console.log (anyString.substring ( 3,nan));//indexend = nan, then Indexend = 0console.log (anystring.substring ( -2,3));//indexStart < 0, then Indexstart = 0console.log (anystring.substring (nan,3));//indexStart = NaN, the indexstart = 0// output "Lla" Console.log (anystring.substring (4,7)); Console.log ( Anystring.substring (7,4));//indexstart > indexend, like two parameters, ditto// output "" Console.log ( Anystring.substring (bis));//indexstart = indexend// output "Mozilla" Console.log ( Anystring.substring (0,7)); Console.log (Anystring.substring (0,10));//indexend > Anystring.length,indexend=anystring.length. Console.log (anystring.substring (0));//Omit indexend, extract the word selector until the end of the string
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
2) substr Description
Returns a substring from the specified position to the specified length.
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var str = "ABCDEFGHIJ";///Length 10// output bcconsole.log (str.substr);// output Hi,start=7console.log ( Str.substr ( -3,2));//start < 0, is regarded as strlength + start, of which strLength // Output Hijconsole.log (STR.SUBSTR ( -3)) for the length of the string,//start < 0, omitting length// Output Bcdefghijconsole.log (STR.SUBSTR (1));// output Ab start=0console.log (STR.SUBSTR ( -20,2)); //abs ( Start) > length of the string,start=0// output "" Console.log (Str.substr (20,2)); //start >= Length of string, output empty string// output "" Console.log (Str.substr (1,0)); Console.log (Str.substr (1,-5));//length <= 0, returns an empty string Console.log (Str.substr (1,nan)),//end non-digital, end=0// output Aconsole.log (Str.substr (NaN, 1));//start non-numeric, start=0
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
Second, slice and splice
slice
| TD style= "Font-size:12px;border-color: #C0C0C0; border-collapse:collapse;padding:3px;" >
splice |
|
a shallow copy of a part of the array is returned to the new array object selected from start to end (not included) |
|
syntax |
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201702/ 211606-20170207194212354-1575944145.png "style=" border:0px;margin-top:10px; "/> |
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/211606/201702/ 211606-20170207194134526-1428088417.png "style=" border:0px;margin-top:10px; "/> |
parameter |
begin: optional, start the extracted index (starting at 0), can be negative end: optional, ends the extracted index (contains begin, does not contain end), can be negative |
start: Index to start modifying (starting at 0), negative deletecount: Optional, item1,item2 ... : optional, elements to be added into the array, from Start position start |
Return |
A new array containing the extracted elements |
An array of elements that are deleted If no element is deleted, an empty array is returned. |
Describe |
1. The original array was not modified, shallow copied the elements of the original array 2. Any parameter is negative, changing the calculation, from the inverse of the array 3. any parameter is Nan, is treated as 0 4. start omitted, start=0 5. start > End, Output empty array |
1. the original array is modified to add any number of elements in the specified position 2. Start <0 is a negative number, changing the calculation method, from the inverse of the array 3. Start or deleteCount is Nan, it is treated as 0 4. start omitted, output an empty array 5. deletecount > array length, deletecount= array length |
Code |
Online execution |
|
1) Slice Description
Returns the shallow copy of the part of the array to the new array object selected from the start to the end (not included), and the original array does not change.
650) this.width=650, "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var fruits = [' Banana ', ' Orange ', ' Lemon ', ' Apple ', ' Mango '];var Citrus = fruits.slice (1, 3);//contains 1 does not contain 3// output [' Banana ', ' Orange ', ' Lemon ', ' Apple ', ' Mango ']console.log (fruits);//original array not modified// output [' Orange ', ' Lemon '] Console.log (Citrus);//Shallow copy elements of the original array// output [' Banana ', ' Orange ', ' Lemon ', ' Apple ', ' Mango ']console.log (Fruits.slice ());// start omit,start=0// output [' Apple ', ' Mango '] Console.log (Fruits.slice ( -2))// start < 0, from the inverse of the array// output [' Apple ']console.log ( Fruits.slice ( -2, -1));// start < 0 && end < 0, from the inverse of the array, The second-to-last element to the penultimate element// output [' Orange ', ' Lemon ', ' Apple ']console.log (Fruits.slice (1, -1));// Output []console.log (Fruits.slice (2, 1));// start > end, output empty array// output [' Banana ']console.log (Fruits.slice (nan, 1));// start non-digital,start=0// output []console.log (Fruits.slice (1, nan));// end non-digital, end=0
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
2) Splice Description
The original array changes by deleting the existing elements and/or adding new elements to change the contents of the array.
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
var myfish = ["Angel", "Clown", "Mandarin", "sturgeon"];// output [] Console.log (Myfish.splice (2, 0, "drum"));// empty, without deleting the element console.log (myFish);//The original array is changed myfish=[" Angel ", " Clown ", " drum ", " Mandarin ", " sturgeon "]// output [" drum "]console.log ( Myfish.splice (2, 1));// has delete element myfish=["Angel", "Clown", "Mandarin", "sturgeon"] Myfish.splice (2, 1, "splice", "parrot"); Console.log (myFish);//Add two elements myfish=["Angel", "Clown", "splice", "Parrot", "sturgeon"]// output ["parrot", "Sturgeon" Console.log (Myfish.splice ( -2))// start < 0, counting from the inverse of the array myfish=["Angel", "Clown", "Splice"]// output []console.log (Myfish.splice ( -2, -1));// start < 0 && deletecount < 0, empty array console.log (Myfish.splice (1, -1));//Empty array console.log ( Myfish.splice (1, nan));// deletecounT non-numeric, Deletecount=0console.log (Myfish.splice ()),// start omit// output ["Angel"]console.log ( Myfish.splice (nan, 1));// start non-numeric, start=0 myfish=["clown", "splice"]// output ["Clown", "Splice"]console.log (Myfish.splice (0, 10));// deletecount > array length
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin-top:10px;border:none; "/>
Comparison of several similar methods in JavaScript