javascript截取字串(支援中英文混合)
程式碼如下:
}
alert(sub('字串截取Javascript處理總結(Js截取中文字串總結)',15))
複製程式碼
碼str = "0123456789";
alert(str.substring(0));------------"0123456789"
alert(str.substring(5));--- ---------"56789"
alert(str.substring(10));-----------""
alert(str.substring(12)) ;-----------""
alert(str.substring(-5));-----------"0123456789"
alert(str.substring (-10));----------"0123456789"
alert(str.substring(-12));----------"0123456789"
alert (str.substring(0,5));----------"01234"
alert(str.substring(0,10));---------"0123456789 "
alert(str.substring(0,12));---------"0123456789"
alert(str.substring(2,0));------- ---"01"
alert(str.substring(2,2));----------""
alert(str.substring(2,5));-- --------"234"
alert(str.substring(2,12));---------"23456789"
alert(str.substring(2,- 2));---------"01"
alert(str.substring(-1,5));---------"01234"
alert(str .substring(-1,-5));--------""
用js方法substr()
程式碼如下:
var str = "0123456789";
alert(str.substr(0));------ ---------"0123456789"
alert(str.substr(5));---------------"56789"
alert(str. substr(10));--------------""
alert(str.substr(12));--------------" "
alert(str.substr(-5));--------------"0123456789"
alert(str.substr(-10));---- ---------"0123456789"
alert(str.substr(-12));-------------"0123456789"
alert(str.substr (0,5));-------------"01234"
alert(str.substr(0,10));------------ "0123456789"
alert(str.substr(0,12));------------"0123456789"
alert(str.substr(2,0));-- -----------""
alert(str.substr(2,2));-------------"23"
alert(str .substr(2,5));-------------"23456"
alert(str.substr(2,12));---------- --"23456789"
alert(str.substr(2,-2));------------""
alert(str.substr(-1,5)) ;------------"01234"
alert(str.substr(-1,-5));-----------""