It is often encountered in the development, and the string is too long and the problem cannot be fully displayed.
At this time, you need to intercept the length we need, and you will display the voices or other characters later.
Since the Chinese characters occupy one byte, the English characters occupy one byte, so simple judgment characters, the effect is often not satisfactory
The following method is intercepted by judging the type of characters, the effect is still possible :)
If you have other solutions welcome, learn together :)
*********************************************************** *******************
PRIVATE STRING STR;
Private Int CounterofDoublebyTe;
Private Byte B [];
/ **
* Set the string that needs to be restricted
* @Param STR needs a string that is restricted
* /
Public void setlimitLengthString (String str) {
THIS.STR = STR;
}
/ **
* @Param LEN requires the length of display ( Note: The length is byte, one Chinese is 2 Byte font>)
* @Param Symbol is used to represent the characters of the omitted information, such as "...", ">>>", etc.
* @Return returns the derived string
* /
Public String getLimitLengthstring (int LEN, STRING SYMBOL) THROWS unsupportedEncodingexception {
CounterofdoubleByte = 0;
B = str.getbytes ("GBK");
IF (B.Length <= LEN)
Return Str;
For (int i = 0; i IF (B <0) CounterofdoubleByte ; } IF (counterofdoublebyte% 2 == 0) Return New String (B, 0, Len, "GBK") Symbol; Else Return New String (B, 0, LEN - 1, "GBK") Symbol; } This article is reposted from users: Focus2004 article