slice(start,end)方法提取字符串的一部分並返回一個新字符串。
使用方法
一般來說該方法有兩個參數,使用方法如下:
slice(start,end) start表示要提取的片段的起始下標,end表示提取片段最后一個字符的后一個字符的下標;
1.兩個參數都為正數
var str="Hello happy world!" document.write(str.slice(6,11);
運行結果: 'happy'
2.若end參數未指定,則選取從start下標開始的所有字符
var str="Hello happy world!" document.write(str.slice(6);
運行結果: 'happy world!'
3.參數若是負數,則表示從字符串尾部開始算起
var str = "Hello happy world!"
3.1.slice(-1)就表示字符串的最后一個字符
運行結果:'!'
3.2.slice(-2)表示字符串的倒數第二個字符
運行結果:'d!'
3.3.slice(3, -2) 表示從第四位開始到倒數第二位
運行結果:'lo happy worl'
參考---https://blog.csdn.net/qq_41576643/article/details/107364239