In JavaScript we can get any part of a string through the slice function, and the slice function needs to pass in two parameters P1 and P2. P1 specifies the starting position of the substring, p2 specifies the length of the string to intercept. Note: The position of the first character in JavaScript is 0. The first example is that we want to intercept the main string of 10 characters starting at 0, so we can use slice (0,10)
In JavaScript we can get any part of a string through the slice function, and the slice function needs to pass in two parameters P1 and P2. P1 specifies the starting position of the substring, p2 specifies the length of the string to intercept. Note: The position of the first character in JavaScript is 0. The first example is that we want to intercept the main string from 0 to 10 characters, so we can use slice (0,10). The following example intercepts 7 characters from the beginning of a string
1 var my_str= "Welcome to Www.ty300.com";
2 var my_slice=my_str.slice (0,7);
3 document.write (My_slice);
The return result of this code is: Welcome.
We can pass a negative number to the parameter P2, which means let the slice function delete the last n characters of the string to see a JS code:
1 var my_str= "Welcome to Www.sharejs.com"
2 var my_slice=my_str.slice (0,-7);
3 document.write (My_slice);
The above JS code output is: Welcome to Qkxue.net,
You must have found out that the last 7 characters were deleted (sliced).
Manuscripts: Diligent Learning qkxue.net
Read the full version of JavaScript slice string slicing function
JavaScript Slice string slicing function