截取定长的字符

字符串截断与字符计数
 1None.gif<script>
 2ExpandedBlockStart.gifContractedBlock.gif/**//*以指定的长度得到字符串,余下的以“dot.gif”代替
 3InBlock.gif参数str是要显示的字符串;strlen是显示的长度,也就是截取的长度
 4ExpandedBlockEnd.gif该函数返回一个字符串*/

 5None.giffunction is_zw(str)
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    exp=/[0-9a-zA-Z_]/g;
 8InBlock.gif    if(str.search(exp) != -1)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        return false;
11ExpandedSubBlockEnd.gif    }

12InBlock.gif    return true;
13ExpandedBlockEnd.gif}

14ExpandedBlockStart.gifContractedBlock.giffunction cutStr(str,strlen)dot.gif{
15InBlock.gif var l,t,c;
16InBlock.gif l = str.length;
17InBlock.gif t = 0;
18ExpandedSubBlockStart.gifContractedSubBlock.gif for(i=0;i<l;i++)dot.gif{
19InBlock.gif 
20ExpandedSubBlockStart.gifContractedSubBlock.gif  c = str.substring(i,i+1); /**//*获取第一个字符,并判断它是字母还是汉字*/
21InBlock.gif  
22ExpandedSubBlockStart.gifContractedSubBlock.gif  if(is_zw(c))dot.gif{               /**//*如果是汉字,步长加2*/
23InBlock.gif   t = t + 2;
24ExpandedSubBlockStart.gifContractedSubBlock.gif  }
elsedot.gif{
25ExpandedSubBlockStart.gifContractedSubBlock.gif   t = t + 1;          /**//*如果是字母,步长加1*/
26ExpandedSubBlockEnd.gif  }

27InBlock.gif  
28ExpandedSubBlockStart.gifContractedSubBlock.gif  if(t>= strlen)dot.gif{
29InBlock.gif   return str.substring(0,i+1);
30ExpandedSubBlockEnd.gif  }

31InBlock.gif  
32ExpandedSubBlockEnd.gif }

33InBlock.gif return str;
34ExpandedBlockEnd.gif}

35None.gif
36None.gif</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值