Common display methods for String Length Truncation

Source: Internet
Author: User

There are ways to find and use them online:

1. CSS Method<I personally think several of the better methods can reduce the number of backendProgramService>

 <!  Doctype   Html   Public   "-// W3C // dtd html 4.0 transitional // en"  >  <  Html  >   <  Head  >    <  Style  > . P_name {padding-left: 0px; overflow: hidden; width: 90px; Word-break: Break-all; Height: 40px}</  Style  >   </  Head  >   <  Body  >   <  Div   Class  = "P_name"  > Sssssssssssssssssssssssssssssssss <  BR  > Ssssssssssssssssssssssssssssssssssssssss </  Div  >  </  Body  >  </  Html  > 
 
2. UseRegular Expression DifferentiationDubyte Character Calculation in Chinese and EnglishGood
 Public static string Getfirststring ( String Stringtosub, Int Length) {RegEx = New RegEx ("[\ u4e00-\ u9fa5] +", regexoptions. Compiled ); Char [] Stringchar = stringtosub. tochararray (); stringbuilder sb = New Stringbuilder (); Int Nlength = 0; Bool Iscut = False ; For ( Int I = 0; I <stringchar. length; I ++ ){ If (RegEx. ismatch (stringchar [I]). tostring () {sb. append (stringchar [I]); nlength + = 2 ;} Else {Sb. append (stringchar [I]); nlength = nlength + 1 ;} If (Nlength> length) {iscut = True ; Break ;}} If (Iscut) Return SB. tostring () + ".."; Else return SB. tostring ();}
 
3. Encoding bytes
 Public String Getlimitedstr ( String Asrcstr, Int Alimitednum ){ If (Alimitednum <= 0)Return Asrcstr; // String tmpstr = removehtmlstr (asrcstr ); String tmpstr = asrcstr; Byte [] Tmpstrbytes = system. Text. encoding. getencoding ("gb2312"). getbytes (tmpstr ); If (Tmpstrbytes. Length <= alimitednum) Return Asrcstr; Else { Byte [] Lstrbytes = Null ; Int Needstrnum = 0; If (Tmpstrbytes [alimitednum]> 127) {lstrbytes = New byte [Alimitednum + 1]; needstrnum = alimitednum + 1 ;} Else {Lstrbytes = New byte [Alimitednum]; needstrnum = alimitednum;} array. Copy (tmpstrbytes, lstrbytes, needstrnum ); Return System. Text. encoding. getencoding ("gb2312"). getstring (lstrbytes );}}

4. other online circulation

  Public static string  subcomment ( string  original,  int  width) { int  Len = original. length;  If  (LEN 
  
    return  original; 
    int  clen = 0; 
    // current length  
    int  cwidth = 0; 
    // current width  
    while  (clen 
   
     If  (
     int ) original [clen]> 128) cwidth ++; clen ++; cwidth ++ ;}< span style = "color: Blue"> return  original. substring (0, clen) ;}
   
  

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.