JavaScript slice
JavaScript slice can extract a certain string from a string. Its usage is similar to JavaScript's substring and substr functions, but the special feature of slice is that it can calculate the position from the end of the string, which is slightly more flexible. . In addition to processing strings, JavaScript slices are usually used to process arrays. In JavaScript Array arrays , you can arbitrarily take out the array value of a certain segment. Please refer to JavaScript Array slice for how slices are used in arrays .
JavaScript slice basic syntax
String.slice( Start, End)
The beginning String in the syntax is the original string. The "Start" and "End" in the parentheses of the slice function are used to indicate the range to be captured. Both can be negative numbers. If Start is a negative number, it means from the string Starting from the very end, -1 represents the last word, -2 represents the second to last word, -3 represents the third to last word, and so on, and the concept of End is the same. If End is not filled in, it represents the slice function The formula starts from the word Start of the string and extracts to the last word of the string.
JavaScript slice example
var NewString = "Good morning.";
document.write(NewString+'<br>');
document.write(NewString. slice(5) +'<br>');
document .write(NewString. slice(1,3) +'<br>');
document.write(NewString. slice(-2,-1) +'<br>');
document.write(NewString);
</ script>

example. At the beginning, we prepare a new string NewString, and output the result on the screen through document.write .
- Output the original string result for the first time
- The second output of the result of slice(5) does not include the fifth position, that is, starting from the word m of the string. Since End is not set, the slice function will extract all strings after the start of m.
- The End position is set for the third slice(1,3), the captured result does not include the End position, so the result is "oo".
- The fourth output adopts the setting of negative parameters, intercepting from the second position of the count to the first position of the count, the result is only a single word "g".
- Output the original string result for the fifth time
Post a Comment
0 Comments