JS按字节截取字符长度实例_javascript技巧
程序员文章站
2022-05-19 13:35:49
...
<P><BR>*<BR> * 处理过长的字符串,截取并添加省略号<BR> * 注:半角长度为1,全角长度为2<BR> * <BR> * pStr:字符串<BR> * pLen:截取长度<BR> * <BR> * return: 截取后的字符串<BR> *<BR><div class="codetitle"><span><a style="CURSOR: pointer" data="30021" class="copybut" id="copybut30021" onclick="doCopy('code30021')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code30021"><BR>function autoAddEllipsis(pStr, pLen) { <br><br> var _ret = cutString(pStr, pLen); <BR> var _cutFlag = _ret.cutflag; <BR> var _cutStringn = _ret.cutstring; <br><br> if ("1" == _cutFlag) { <BR> return _cutStringn + "..."; <BR> } else { <BR> return _cutStringn; <BR> } <BR>} <BR></div> <BR>*<BR> * 取得指定长度的字符串<BR> * 注:半角长度为1,全角长度为2<BR> * <BR> * pStr:字符串<BR> * pLen:截取长度<BR> * <BR> * return: 截取后的字符串<BR> *<BR><div class="codetitle"><span><a style="CURSOR: pointer" data="21735" class="copybut" id="copybut21735" onclick="doCopy('code21735')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code21735"><BR>function cutString(pStr, pLen) { <br><br> // 原字符串长度 <BR> var _strLen = pStr.length; <br><br> var _tmpCode; <br><br> var _cutString; <br><br> // 默认情况下,返回的字符串是原字符串的一部分 <BR> var _cutFlag = "1"; <br><br> var _lenCount = 0; <br><br> var _ret = false; <br><br> if (_strLen <= pLen/2) { <BR> _cutString = pStr; <BR> _ret = true; <BR> } <br><br> if (!_ret) { <BR> for (var i = 0; i < _strLen ; i++ ) { <BR> if (isFull(pStr.charAt(i))) { <BR> _lenCount += 2; <BR> } else { <BR> _lenCount += 1; <BR> } <br><br> if (_lenCount > pLen) { <BR> _cutString = pStr.substring(0, i); <BR> _ret = true; <BR> break; <BR> } else if (_lenCount == pLen) { <BR> _cutString = pStr.substring(0, i + 1); <BR> _ret = true; <BR> break; <BR> } <BR> } <BR> } <br><br> if (!_ret) { <BR> _cutString = pStr; <BR> _ret = true; <BR> } <br><br> if (_cutString.length == _strLen) { <BR> _cutFlag = "0"; <BR> } <br><br> return {"cutstring":_cutString, "cutflag":_cutFlag}; <BR>} <BR></div> <BR>*<BR> * 判断是否为全角<BR> * <BR> * pChar:长度为1的字符串<BR> * return: tbtrue:全角<BR> * false:半角<BR> *</P>
<P><div class="codetitle"><span><a style="CURSOR: pointer" data="99709" class="copybut" id="copybut99709" onclick="doCopy('code99709')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code99709"> <BR>function isFull (pChar) {<BR> for (var i = 0; i < pChar.strLen ; i++ ) { <BR> if ((pChar.charCodeAt(i) > 128)) { <BR> return true; <BR> } else { <BR> return false; <BR> }<BR>}<BR>}<BR></div><BR>用例:<BR><div class="codetitle"><span><a style="CURSOR: pointer" data="39229" class="copybut" id="copybut39229" onclick="doCopy('code39229')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code39229"><BR>testStr = "测试1字符串";<BR>autoAddEllipsis(testStr, 1); // "测..."<BR>autoAddEllipsis(testStr, 2); // "测..."<BR>autoAddEllipsis(testStr, 3); // "测..."<BR>autoAddEllipsis(testStr, 4); // "测试..."<BR>autoAddEllipsis(testStr, 5); // "测试1..."<BR>autoAddEllipsis(testStr, 6); // "测试1..."<BR>autoAddEllipsis(testStr, 7); // "测试1字..."<BR></div></P>
推荐阅读
-
JS字符串长度判断,超出进行自动截取的实例(支持中文)
-
js中根据字数截取字符串,不能截断url_javascript技巧
-
js prototype截取字符串函数_javascript技巧
-
js prototype截取字符串函数_javascript技巧
-
标题过长使用javascript按字节截取字符串_javascript技巧
-
JS截取字符串常用方法整理及使用示例_javascript技巧
-
JS截取字符串常用方法详细整理_javascript技巧
-
Json字符串转换为JS对象的高效方法实例_javascript技巧
-
JS截取字符串常用方法详细整理_javascript技巧
-
JS按字节截取字符长度实例_javascript技巧