JS to intercept character length instances by byte _javascript tips

Source: Internet
Author: User


*
* 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 ..."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.