Topic: The last time I wrote a tag, I captured the string by byte on the JSP page. The boss said it was not ideal. I thought about it carefully. I had no experience and changed it.
Principle: it is not as complex as it is mentioned on the Internet. A single byte is 8 bits, the maximum binary value is 11111111, and the maximum decimal value is255, ASCII code less than 255 is a single byte, more than two bytes are processed.
Static implementation:
/** <Br/> * @ Param STR: <br/> * Source string <br/> * @ Param width: <br/> * string's byte width <br/> * @ Param ellipsis: <br/> * a string added to abbreviate string bottom <br/> * @ return String object <br/> * @ Author X. ING's blog <br/> * @ See <a href = "http://blog.csdn.net/xxd851116/"> X. ING's blog </a> <br/> */<br/> Public static string abbreviate (string STR, int width, string ellipsis) {<br/> If (S Tr = NULL) {<br/> return ""; <br/>}</P> <p> int D = 0; // byte length <br/> int n = 0; // char length <br/> for (; n <Str. length (); N ++) {<br/> d = (INT) Str. charat (n)> 256? D + 2: D + 1; <br/> If (D> width) {<br/> break; <br/>}</P> <p> If (D> width) {<br/> N = N-ellipsis. length ()/2; <br/> return Str. substring (0, N> 0? N: 0) + ellipsis; <br/>}</P> <p> return STR = Str. substring (0, n); <br/>}
Reprinted please indicate the source: http://blog.csdn.net/xxd851116/archive/2009/08/30/4498457.aspx