in a programming language , strings can be said is the most common type , and the string in the program operation is also very frequent. When the program comes with a variety of string manipulation language method, using the programming language program when there is a lot of convenience, improve development efficiency. But when the method too much , or even similar purpose , parameters identical time, it is likely to cause confusion difficult to choose the dilemma .
In JavaScript there is such a situation , take the string for a substring operation , JavaScript provides three different ways : slice , substr , substring . While searching online casual look , you can find the article describes the difference between the three , but each time you use it , still confused no choice. Therefore, the difference between binding online presentation , in this paper the similarities and differences between them will be listed in the table for easy reference and distinction.
test string : "0123456789"
< / span>
browsers : Chrome Version 29.0.1547.76
operating System : Mac OS X Version 10.8.5
parameter | method | ||||
parameter 1 | parameter 2 | slice (begin [, end]) | substr (start [, length]) | substring (from [, to]) | |
Parameter / Result | 7 | 789 | 789 | 789 | |
Description | only parameter 1 , omit parameter 2 | from parameter 1 specifies the coordinates of the end of the string interception | |||
Parameter / Result | -3 | 789 | 789 | 0123456789 | |
Description | parameter 1 is negative | calculated from the end of the string position * | as 0 | ||
Parameter / Result | 21 | ||||
Description | parameter 1 is greater than or equal to the length of the string | as string length, that returns an empty string | |||
Parameter / Result | "3.21" | 3456789 | 3456789 | 3456789 | |
Description | parameter 1 to float or containing only numbers string | like parseInt () converts a numeric value to an integer | |||
Parameter / Result | NaN | 0123456789 | 0123456789 | 0123456789 | |
Description | parameter 1 non- valid value | regarded as non- valid value 0 | |||
Parameter / Result | 3 | 7 | 3456 | 3456789 | 3456 |
Description | both parameter 1 , there parameter 2 | from parameter 1 position to intercept parameter 2 position | from parameter 1 location interception length is parameter 2 characters | same slice () | |
Parameter / Result | 7 | 3 | 789 | 3456 | |
Description | parameter 1 is greater than parameter 2 | returns an empty string | from parameter 1 location interception length is parameter 2 characters | exchange parameter 1 and parameter 2 and then the interception | |
Parameter / Result | 3 | -3 | 3456 | 012 | |
Description | parameter 2 is negative | negative characters from the start to the end position | as length 0 , returns an empty string | exchange parameters , negative as 0 | |
Parameter / Result | 3 | 21 | 3456789 | 3456789 | 3456789 |
Description | parameter 2 is greater than or equal to the length of the string | as string length , the results with omitted parameters 2 | |||
Parameter / Result | 3 | NaN | 012 | ||
Description | parameter 2 non- valid value | non- valid value as 0 , returns an empty string | non- valid value as 0 , because the empty string of length 0 | non- valid value as 0 , the exchange parameter interception | |
Parameter / Result | -7 | -3 | 3456 | ||
Description | parameter 1 and parameter 2 is negative | calculated from the end of the string position | length is negative, returns an empty string | negative as 0 , returns an empty string | |
Parameter / Result | -7 | 7 | 3456 | 3456789 | 0123456 |
Description | parameter 1 is a positive number , parameter 2 is negative | ( automatically applied on top of the rules , do not explain ) |
* calculate the position from the end of the string : -1 refers to the last character in the string , -2 means the second last character , and so on ; also visible string length is the sum of the negative of the position, if the result is less than 0 , it is considered 0 .
contrast from the above table is not difficult to see that the main difference between the three methods are as follows :
- three method parameter represents a substring starting position , parameter 2 , said the slice and substring end position , and in behalf of substr is a substring of length ;
- For negative attitude , when there is a position in the parameter , slice , and substr from the end , and the end of substring does not support direct counting method as 0 ; When the parameter 2 position , slice and substring of dealing with parameter 1 : the former from the end , which is converted to 0, and then , as the negative substr returns an empty string of length 0 ;
- parameter 2 for parameter 1 is less than the case , substring biggest difference is that it will exchange two parameters and then intercept substring , substr because the second parameter indicates the length therefore no exception , slice Knots whole story is still normal substring search position, if the starting position at the end behind the empty string is returned .
According MDN right substr description , in IE may not support negative calculated from the end way .
没有评论:
发表评论