当前位置 : 主页 > 网络编程 > JavaScript >

subString.txt

来源:互联网 收集:自由互联 发布时间:2021-07-03
subString.txt /* * 截取字符串 包含中文处理 * (串,长度,增加...) */ function subString(str, start, len, hasDot) { var newLength = 0; var newStr = ""; var chineseRegex = /[^\x00-\xff]/g; var singleChar = ""; var strLength = str
subString.txt
/* 
* 截取字符串 包含中文处理  
* (串,长度,增加...)  
*/ 
function subString(str, start, len, hasDot)  
{  
    var newLength = 0;  
    var newStr = "";  
    var chineseRegex = /[^\x00-\xff]/g;  
    var singleChar = "";  
    var strLength = str.replace(chineseRegex,"**").length; 
    for(var i = (start-1);i < strLength;i++)  
    {  
        singleChar = str.charAt(i).toString();  
        if(singleChar.match(chineseRegex) != null)  
        {  
            newLength += 2;  
        }      
        else  
        {  
            newLength++;  
        }  
        if(newLength > len)  
        {  
            break;  
        }  
        newStr += singleChar;  
    }  

    if(hasDot && strLength > len)  
    {  
        newStr += "...";  
    }  
    return newStr;  
}
网友评论