An overview of the
Substr or substring can be used to slice a string. They can both accept two arguments. The first argument is the position at which the string is truncated, and the default is 0. Github.com/fanxuewen/e…
Pass only the first parameter
When the first parameter is positive, they are indistinguishable
let str='Good good study, day day up';
console.log('substr---:',str.substr(2));
console.log('substring:',str.substring(2));
console.log('slice----:',str.slice(2))
Copy the code
let str='Good good study, day day up';
console.log('substr---:',str.substr(-2));
console.log('substring:',str.substring(-2));
console.log('slice----:',str.slice(-2));
Copy the code
Two, both parameters pass
If both parameters are positive and the second is greater than the first. Substr is truncated from where the first argument begins. The second argument represents the length of the truncation. The substring and slice parameters represent the index of the intercept location, including the start but not the end
let str='Good good study, day day up';
console.log('substr---:', STR., substr (2, 4)); console.log('substring:', STR. The substring (2, 4)); console.log('slice----:', STR. Slice (2, 4));Copy the code
small
let str='Good good study, day day up';
console.log('substr---:', STR., substr (2, 1)); console.log('substring:', STR. The substring (2, 1)); console.log('slice----:', STR. Slice (2, 1));Copy the code
Other cases to verify……..