==========================================
The string with... is intercepted to ensure that the length of all characters (2 in Chinese) cannot exceed Max:
Example:
Left ("abcdefg", 7) = "abcdefg"
Left ("abcdefg", 6) = "ABC ..."
Left ("Chinese abcdefg", 9) = "Chinese AB ..."
Package com. crackj2ee. util;
/**
* @ Author Xuefeng, asklxf@163.com
*/
Public class stringutil {
Public static string left (string S, int max ){
Char [] cs = S. tochararray ();
Int COUNT = 0;
Int last = cs. length;
For (INT I = 0; I <CS. length; I ++ ){
If (CS [I] & gt; 255)
Count + = 2;
Else
Count ++;
If (count> MAX ){
Last = I + 1;
Break;
}
}
If (count <= max) // string is short or just the size!
Return S;
// String is too long:
Max-= 3;
For (INT I = last-1; I> = 0; I --){
If (CS [I] & gt; 255)
Count-= 2;
Else
Count --;
If (count <= max ){
Return S. substring (0, I) + "...";
}
}
Return "...";
}
}
......
[Read the full text]