欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JS按字节截取字符长度实例_javascript技巧

程序员文章站 2022-05-19 13:35:49
...
<P><BR>*<BR>&nbsp;* 处理过长的字符串,截取并添加省略号<BR>&nbsp;* 注:半角长度为1,全角长度为2<BR>&nbsp;* <BR>&nbsp;* pStr:字符串<BR>&nbsp;* pLen:截取长度<BR>&nbsp;* <BR>&nbsp;* return: 截取后的字符串<BR>&nbsp;*<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>&nbsp;&nbsp;&nbsp; var _ret = cutString(pStr, pLen); <BR>&nbsp;&nbsp;&nbsp; var _cutFlag = _ret.cutflag; <BR>&nbsp;&nbsp;&nbsp; var _cutStringn = _ret.cutstring; <br><br>&nbsp;&nbsp;&nbsp; if ("1" == _cutFlag) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return _cutStringn + "..."; <BR>&nbsp;&nbsp;&nbsp; } else { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return _cutStringn; <BR>&nbsp;&nbsp;&nbsp; } <BR>} <BR></div> <BR>*<BR>&nbsp;* 取得指定长度的字符串<BR>&nbsp;* 注:半角长度为1,全角长度为2<BR>&nbsp;* <BR>&nbsp;* pStr:字符串<BR>&nbsp;* pLen:截取长度<BR>&nbsp;* <BR>&nbsp;* return: 截取后的字符串<BR>&nbsp;*<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>&nbsp;&nbsp;&nbsp; // 原字符串长度 <BR>&nbsp;&nbsp;&nbsp; var _strLen = pStr.length; <br><br>&nbsp;&nbsp;&nbsp; var _tmpCode; <br><br>&nbsp;&nbsp;&nbsp; var _cutString; <br><br>&nbsp;&nbsp;&nbsp; // 默认情况下,返回的字符串是原字符串的一部分 <BR>&nbsp;&nbsp;&nbsp; var _cutFlag = "1"; <br><br>&nbsp;&nbsp;&nbsp; var _lenCount = 0; <br><br>&nbsp;&nbsp;&nbsp; var _ret = false; <br><br>&nbsp;&nbsp;&nbsp; if (_strLen &lt;= pLen/2) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _cutString = pStr; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ret = true; <BR>&nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp;&nbsp; if (!_ret) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (var i = 0; i &lt; _strLen ; i++ ) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isFull(pStr.charAt(i))) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _lenCount += 2; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _lenCount += 1; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_lenCount &gt; pLen) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _cutString = pStr.substring(0, i); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ret = true; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else if (_lenCount == pLen) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _cutString = pStr.substring(0, i + 1); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ret = true; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>&nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp;&nbsp; if (!_ret) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _cutString = pStr; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _ret = true; <BR>&nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp;&nbsp; if (_cutString.length == _strLen) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _cutFlag = "0"; <BR>&nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp;&nbsp; return {"cutstring":_cutString, "cutflag":_cutFlag}; <BR>} <BR></div> <BR>*<BR>&nbsp;* 判断是否为全角<BR>&nbsp;* <BR>&nbsp;* pChar:长度为1的字符串<BR>&nbsp;* return: tbtrue:全角<BR>&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; false:半角<BR>&nbsp;*</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>&nbsp; for (var i = 0; i &lt; pChar.strLen ; i++ ) {&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; if ((pChar.charCodeAt(i) &gt; 128)) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true; <BR>&nbsp;&nbsp;&nbsp; } else { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false; <BR>&nbsp;&nbsp;&nbsp; }<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>