JS automatically intercept string length, add ellipsis "..."

Source: Internet
Author: User

The JavaScript string handler function, which intercepts the string according to the length of the definition, cuts out the append ... many times the content displayed on the page needs to be indented "..." This method is used to process the string display fixed length, the extra-long part with "..." instead of:

/** Parameter Description: * Based on length interception first use string, Extra long part append ... * str object string * len target byte length * return value: Process result string*/functioncutstring (str, len) {//the Length property reads 1 Chinese characters .    if(Str.length*2 <=Len) {        returnstr; }    varstrlen = 0; vars = "";  for(vari = 0;i < str.length; i++) {s= S +Str.charat (i); if(Str.charcodeat (i) > 128) {strlen= strlen + 2; if(Strlen >=Len) {                returnS.substring (0,s.length-1) + "..."; }        } Else{strlen= strlen + 1; if(Strlen >=Len) {                returnS.substring (0,s.length-2) + "..."; }        }    }    returns;}

JS automatically intercept string length, add ellipsis "..."

Related Article

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.