javascript截取字符串(通过substring实现并支持中英文混合)
javascript截取字符串(支持中英文混合)
用js方法substring()
用js方法substr()
代码如下:
<script type="text/javascript"> var sub=function(str,n){ var r=/[^\x00-\xff]/g; if(str.replace(r,"mm").length<=n){return str;} var m=Math.floor(n/2); for(var i=m;i<str.length;i++){ if(str.substr(0,i).replace(r,"mm").length>=n){ return str.substr(0,i)+"..."; } } return str; } alert(sub('字符串截取Javascript处理总结(Js截取中文字符串总结)',15)) </script>
用js方法substring()
代码如下:
var 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));-----------""
相关推荐
一像素 2013-04-17
sunlizhen 2020-06-01
kuoying 2020-05-12
嗡汤圆 2020-05-02
好记忆也需烂 2020-04-18
heray0 2020-04-15
chouliqingke 2019-12-18
aanndd 2019-12-08
zangdaiyang 2019-12-05
chenxiangis 2019-11-01
ThinkingLink 2019-11-01
TMD咯MySQL 2019-09-05
kxr00 2012-07-14
Ongoingcre 2011-06-08
zjhqlmzldx 2011-05-10
hongsheyoumo 2010-03-02
huha 2009-11-12