Javascript String cutStr(str, len)

Description

Javascript String cutStr(str, len)


function cutStr(str, len) {
    var temp,/* www. j  a v a2  s .co  m*/
        icount = 0,
        patrn = /[^\x00-\xff]/,
        strre = "";
    for (var i = 0; i < str.length; i++) {
        if (icount < len - 1) {

            temp = str.substr(i, 1);
            if (patrn.exec(temp) == null) {
                icount += 1;
            } else {
                icount += 2;
            }
            strre += temp;
        } else {
            break;
        }
    }
    return strre + "...";
}



PreviousNext

Related