在js中截取字符串有两种方法:
1:substring2substr
substring返回一个从指定位置开始的指定长度的子字符串。arr。substring(start,length)
1: substring 方法返回的子串包括 start 处的字符,但不包括 end 处的字符。
如果 start 与 end 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。
如果 start 比 end 大,那么该方法在提取子串之前会先交换这两个参数。
如果 start 或 end 为负数,那么它将被替换为 0。
2: substr返回一个从指定位置开始到指定位置的子字符串。arr.substr(start,end)
如果 length 为 0 或负数,将返回一个空字符串。
如果没有指定该参数,则子字符串将延续到stringObject的最后。
var barcode = 'ITEM000003-2'; console.log(barcode.substring(0,1));--------------------------I console.log(barcode.substring(0,3));--------------------------ITE console.log(barcode.substring(1,4));--------------------------TEM0 console.log(barcode.substring(2,6));--------------------------EM00 console.log(barcode.substring(0,9));--------------------------ITEM00000 console.log(barcode.substring(5,10));------------------------00003 console.log(barcode.substring(3,11));------------------------M000003- console.log(barcode.substring(0,12));------------------------ITEM000003-2 console.log(barcode.substr(0,5));-----------------------------ITEM0 console.log(barcode.substr(0,7));-----------------------------ITEM000 console.log(barcode.substr(1,6));-----------------------------TEM00 console.log(barcode.substr(2,8));-----------------------------EM0000 console.log(barcode.substr(2));-------------------------------EM000003-2 console.log(barcode.substr(3,11));---------------------------M000003-