*
* Handle too long string, intercept and add ellipsis
* Note: Half angle length is 1, full angle length is 2
*
* PSTR: string
* PLen: Intercept length
*
* Return: After the intercepted string
*
Copy Code code as follows:
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;
}
}
*
* Get a string of the specified length
* Note: Half angle length is 1, full angle length is 2
*
* PSTR: string
* PLen: Intercept length
*
* Return: After the intercepted string
*
Copy Code code as follows:
function cutstring (pstr, PLen) {
Original string length
var _strlen = pstr.length;
var _tmpcode;
var _cutstring;
By default, the returned string is part of the original string
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};
}
*
* Judge whether it is full angle
*
* Pchar: A string of length 1
* Return:tbtrue: Full angle
* False: Half Corner
*
Copy Code code as follows:
function Isfull (pchar) {
for (var i = 0; i < Pchar.strlen; i++) {
if ((Pchar.charcodeat (i) > 128)) {
return true;
} else {
return false;
}
}
}
Case:
Copy Code code as follows:
TESTSTR = "Test 1 string";
Autoaddellipsis (TESTSTR, 1); "Test ..."
Autoaddellipsis (TESTSTR, 2); "Test ..."
Autoaddellipsis (TESTSTR, 3); "Test ..."
Autoaddellipsis (TESTSTR, 4); "Test ..."
Autoaddellipsis (TESTSTR, 5); "Test 1 ..."
Autoaddellipsis (TESTSTR, 6); "Test 1 ..."
Autoaddellipsis (TESTSTR, 7); "Test 1 words ..."