JS之截取字符串

一、slice截取字符串

截取倒数第几位的字符串时,可以用slice方便获取

var test_string = "abc123456"
//截取倒数六位字符串
var new_string = test_string.slice(-6);
//截取第一位到第三位字符
var new_string1 = test_string.slice(0,3);

console.log('=====new_string:' + new_string);
console.log('=====new_string1:' + new_string1);

二、substring截取字符串

substring无法倒数截取字符串

var test_string = "abc123456"
//截取第二位到第五字符串
var new_string = test_string.substring(1,6);
//截取第一位到第三字符串
var new_string1 = test_string.substring(0,3);

console.log('=====new_string:' + new_string);
console.log('=====new_string1:' + new_string1);

三、参考

1、https://www.cnblogs.com/yungiu/p/11497946.html
2、https://zhuanlan.zhihu.com/p/162910462
3、https://www.cnblogs.com/lasdaybg/p/9971285.html
4、https://www.jianshu.com/p/cae50f9b61ab

posted @ 2021-11-15 14:24  xyztank  阅读(612)  评论(0)    收藏  举报