<script language="javascript" type="text/javascript">
/**
* js截取字符串,中英文都能用
* @param str:需要截取的字符串
* @param len: 需要截取的长度
*/
function cutstr(str,len)
{
<span style="white-space:pre"> </span>var str_length = 0;
<span style="white-space:pre"> </span>var<span style="white-space:pre"> </span>str_cut = new String();
<span style="white-space:pre"> </span>//如果给定字符串小于指定长度,则返回源字符串;
<span style="white-space:pre"> </span>if(str.length<len){
<span style="white-space:pre"> </span>return str;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(var i = 0;i<str.length;i++){
<span style="white-space:pre"> </span>var a = str.charAt(i);
<span style="white-space:pre"> </span>str_length++;
<span style="white-space:pre"> </span>if(escape(a).length > 4){
<span style="white-space:pre"> </span>//中文字符的长度经编码之后大于4
<span style="white-space:pre"> </span>str_length++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>str_cut = str_cut.concat(a);
<span style="white-space:pre"> </span>if(str_length>=len){
<span style="white-space:pre"> </span>str_cut = str_cut.concat("...");
<span style="white-space:pre"> </span>return str_cut;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
截取中英文字符串长度
最新推荐文章于 2021-06-07 20:33:18 发布