How to process Chinese Characters in C # In two bytes
Because. in. net, C # processes Chinese Characters in one byte by default. The solution is to convert the character C # To be parsed to unicode encoding by default and process Chinese Characters in one byte, this will affect the precise interception of strings with Chinese characters. To precisely intercept bytes, you must first convert unicode encoding to ANSI encoding. You can convert it to a byte array, and then extract bytes according to the array. The specific implementation is as follows:
// Process Chinese Characters in two bytes
Private string gbksubstring (string S, int starindex, int Len)
{
Byte [] bwrite; // byte content
Bwrite = encoding. getencoding ("gb2312"). getbytes (S. tochararray ());
Return encoding. Default. getstring (bwrite, starindex, Len );
}
// Call:
String S = "asdfghjk Chinese people zxcvbnm ";
String S1 = gbksubstring (S, 8, 8); // return "Chinese people"