HTML
<>script> var str = "String segmentation 字串分割" ; var n = 6 ; //分割字數 var strArr=[] ; for( var i=0 ; i < str.length / n ; i++){ var a = str.slice( n*i , n*(i+1) ); strArr.push(a) } <>/script> 分割結果 : JSON 陣列
stringObject.slice(start, end) start : 要取得部份字串的起始。如果是負數, 則該參數規定的是從字串的尾部開始算起的位置。 end : 接著要取得部份字串的結尾。若未指定此參數, 則要提取的子串包括 start 到原字串結尾的字串。 var TxT = "0123456789ABCDEF" TxT.length = 16 TxT.slice( 0) = TxT.slice( 4) = TxT.slice(-4) = TxT.slice(4,6) = TxT.slice(4,30) = TxT.slice(-6,-3) = TxT.slice(6,-3) = TxT.slice(4,TxT.length) =
var TxT = [11,22,33,44,55,66,77,88,99] TxT.length = TxT.slice( 0) = TxT.slice( 4) = TxT.slice(-4) = TxT.slice(4,6) = TxT.slice(4,30) = TxT.slice(-4,-2) = TxT.slice(4,-2) = TxT.slice(4,TxT.length) =
沒有留言:
張貼留言