js字符串的截取:substring substr slice区
substring(x,y):输出一个字符串,当其中只有一个参数时 会输出从x开始到结尾的string 截取的字符串不包括第x个元素 但是包含第y个元素
substring中如果x>y 则会自动调换位置
var str = "hello";
console.log(str.substring(4)); //o
console.log(str.substring(1,5)) //ello
console.log(str.substring(5,1)); //ello
substr(x,y) 其中的x y属性分别代表元素的起始位置及输出的元素长度
console.log(str.substr(2,8)) //llo
slice(x,y)和substring类似,都是返回[x,y]区间的字符串,唯一不同的是如果x>y 则会产生一个空 并不会自动调换位置
console.log(str.slice(1,5)) // ello
console.log(str.slice(5,1)) //空
本文标题:js字符串的截取:substring substr slice区
本文链接:https://www.haomeiwen.com/subject/kmltdktx.html
网友评论