Home > Web Front-end > JS Tutorial > JS intercepts character length by bytes example_javascript skills

JS intercepts character length by bytes example_javascript skills

PHPz
Release: 2020-07-10 17:52:04
Original
1617 people have browsed it


*
* 处理过长的字符串,截取并添加省略号
* 注:半角长度为1,全角长度为2
*
* pStr:字符串
* pLen:截取长度
*
* return: 截取后的字符串
*

复制代码 代码如下:

function autoAddEllipsis(pStr, pLen) {

var _ret = cutString(pStr, pLen);
var _cutFlag = _ret.cutflag;
var _cutStringn = _ret.cutstring;

if ("1" == _cutFlag) {
return _cutStringn + "...";
} else {
return _cutStringn;
}
}

*
* 取得指定长度的字符串
* 注:半角长度为1,全角长度为2
*
* pStr:字符串
* pLen:截取长度
*
* return: 截取后的字符串
*
复制代码 代码如下:

function cutString(pStr, pLen) {

// 原字符串长度
var _strLen = pStr.length;

var _tmpCode;

var _cutString;

// 默认情况下,返回的字符串是原字符串的一部分
var _cutFlag = "1";

var _lenCount = 0;

var _ret = false;

if (_strLen <= pLen/2) {
_cutString = pStr;
_ret = true;
}

if (!_ret) {
for (var i = 0; i < _strLen ; i++ ) {
if (isFull(pStr.charAt(i))) {
_lenCount += 2;
} else {
_lenCount += 1;
}

if (_lenCount > pLen) {
_cutString = pStr.substring(0, i);
_ret = true;
break;
} else if (_lenCount == pLen) {
_cutString = pStr.substring(0, i + 1);
_ret = true;
break;
}
}
}

if (!_ret) {
_cutString = pStr;
_ret = true;
}

if (_cutString.length == _strLen) {
_cutFlag = "0";
}

return {"cutstring":_cutString, "cutflag":_cutFlag};
}

*
* 判断是否为全角
*
* pChar:长度为1的字符串
* return: tbtrue:全角
* false:半角
*

复制代码 代码如下:

function isFull (pChar) {
for (var i = 0; i < pChar.strLen ; i++ ) {
if ((pChar.charCodeAt(i) > 128)) {
return true;
} else {
return false;
}
}
}

用例:
复制代码 代码如下:

testStr = "测试1字符串";
autoAddEllipsis(testStr, 1); // "测..."
autoAddEllipsis(testStr, 2); // "测..."
autoAddEllipsis(testStr, 3); // "测..."
autoAddEllipsis(testStr, 4); // "测试..."
autoAddEllipsis(testStr, 5); // "测试1..."
autoAddEllipsis(testStr, 6); // "测试1..."
autoAddEllipsis(testStr, 7); // "测试1字..."

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template