In C #, Chinese characters are processed in two bytes.

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.