<html>
<body>
<script type="text/javascript">
//str为目标字符串,length为目标长度,中文字符算两个单字节字符
function getSubStrIncludeChinese(str,length){
if(str.length*2<=length){
return str;
}else{
var currentLength = 0;
var resStr = "";
for(var i=0;i<str.length;i++){
if(str.charCodeAt(i)>=0 && str.charCodeAt(i)<=255){
currentLength +=1;
}else{
currentLength +=2;
}
if(currentLength > length){
return resStr;
}
resStr += str[i];
if(currentLength == length){
return resStr;
}
}
return str;
}
}
var str ="付费ds额2fds附属设施"
document.write(getSubStrIncludeChinese(str,14))
</script>
</body>
</html>