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) ;}