Compile a function to intercept a string. The input is a string and number of characters, and the output is a string truncated by bytes. However, make sure that no half of Chinese characters are intercepted. For example, if "my ABC" 4 is used, set it to "my AB" and enter "my ABC Han DEF", 6, the output should be "my ABC" rather than "my ABC + half of the Chinese ".
Public static String substring (String str, int toCount, String more)
{
Int reInt = 0;
String reStr = "";
If (str = null)
Return "";
Char [] tempChar = str. toCharArray ();
For (int kk = 0; (kk <tempChar. length & toCount> reInt); kk ++ ){
String s1 = str. valueOf (tempChar [kk]);
Byte [] B = s1.getBytes ();
ReInt + = B. length;
ReStr + = tempChar [kk];
}
If (toCount = reInt | (toCount = reInt-1 ))
ReStr + = more;
Return reStr;
}
When a Web application displays a string in a browser, due to the display length limitation, it is often necessary to intercept the string before display. However, many popular languages, such as C # and Java, use Unicode 16 (UCS2) encoding internally. In this encoding, all the characters are two characters. Therefore, if the string to be intercepted is a mix of Chinese, English, and numbers, the following string is generated:
String s = "a plus B equals c. If a equals 1 and B equals 2, c equals 3 ";
The preceding string contains both Chinese characters and English characters and numbers. If you want to intercept the first six bytes of characters, it should be "a plus B", but if you use the substring method to intercept the first six characters, it will be "a plus B equals c ". This problem occurs because the substring method treats double-byte Chinese characters as one byte character (UCS2 character. To solve this problem, first obtain the UCS2 encoded byte array of the string. The following code is as follows:
Byte [] bytes = s. getBytes ("Unicode ");
Because the first two bytes in the byte array generated above are flags, bytes [0] =-2, bytes [1] =-1, therefore, scanning starts from the third byte, for an English or numeric character, the second UCS2 encoding byte is the corresponding ASCII, the first byte is 0, for example, a UCS2 encoding is 0 97, the two Chinese characters are not 0 bytes. Therefore, the UCS2 encoding rule can be used to calculate the actual number of bytes. The implementation code of this method is as follows:
Public static String bSubstring (String s, int length) throws Exception
{
Byte [] bytes = s. getBytes ("Unicode ");
Int n = 0; // the current number of bytes.
Int I = 2; // The number of bytes to intercept, starting from 3rd bytes.
For (; I <bytes. length & n <length; I ++)
{
// The odd position, such as 3, 5, and 7, which is the second byte of the two bytes in UCS2 Encoding
If (I % 2 = 1)
{
N ++; // Add 1 to n when the second byte of UCS2
}
Else
{
// When the first UCS2 encoding byte is not equal to 0, the UCS2 character is a Chinese character. One Chinese character is counted as two bytes.
If (bytes [I]! = 0)
{
N ++;
}
}
}
// If I is an odd number, it is processed as an even number.
If (I % 2 = 1)
{
// When the UCS2 character is a Chinese character, remove the half Chinese Character
If (bytes [I-1]! = 0)
I = I-1;
// If the UCS2 character is a letter or number, the character is retained.
Else
I = I + 1;
}
Return new String (bytes, 0, I, "Unicode ");
}
The following code uses the bSubstring method:
String s = "a plus B equals c. If a equals 1 and B equals 2, c equals 3 ";
System. out. println (bSubstring (s, 6 ));
The string intercepted by the above code is "A plus B ".