<script type="text/javascript">
// 截取固定长度子字符串 sSource为字符串iLen为长度
function getInterceptedStr(sSource, iLen)
{
if(sSource.replace(/[^\x00-\xff]/g,"xx").length <= iLen)
{
return sSource;
}
var str = "";
var l = 0;
var schar;
for(var i=0;schar=sSource.charAt(i);i++)
{
str += schar;
l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);
if(l >= iLen)
{
break;
}
}
return str;
}
var str1="这是一个uuuu字符串截取的函数,this is a test!";
//alert(getStrActualLen(str1));
alert(getInterceptedStr(str1,10));
</script>
posted on 2008-07-16 10:38
梓枫 阅读(2484)
评论(1) 编辑 收藏 所属分类:
javascript