String truncation containing Chinese characters and related

Source: Internet
Author: User

Preface:

How to effectively intercept a string containing Chinese characters (based on the number of bytes provided ). For example, extract the first part of the input string based on the number of bytes provided. If the end character is a letter, it is retained. If it is the first byte of the Chinese character, one byte is taken forward. Ex: "I am ABB" 6. The result is "I am AB"; "I am ABB" 8. The result is "I am ABB ".

Because no language (C, C #) is specified #). Let's assume that only Chinese characters and letters are made up.

    • If C is used, this will be a general question (except for processing Chinese characters ).
    • If C # is used, I want to do the following:

//Description: capture a string containing Chinese Characters

//Copyright:Http://www.cnblogs.com/yangmingming

// Notes: completed by calling Functions

Namespace Stringcutdemo
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
String Str1 =   " I am ABC " ;
String Str2 =   " Am I ABC? " ;
String Getstr1 = Getstring (str1, 4 );
String Getstr2 = Getstring (str2, 6 );
Console. writeline (
" Get the str1 and str2 is {0}, {1} " , Getstr1, getstr2 );

}
Public   Static   String Getstring ( String STR, Int Count)
{
If ( ! (STR [count -   1 ] > =   ' A '   && STR [count -   1 ] <=   ' Z '   | STR [count -   1 ] > =   ' A '   && STR [count -   1 ] <=   ' Z ' ))
Return Str. substring ( 0 , Count -   1 );
Else
Return Str. substring ( 0 , Count );
}
}
}

However, the output result is true:

 

 

It can be seen that in C #, in the traditional sense, the ASCII character occupies one byte, and here it has been upgraded to two bytes, with the same number of bytes as the Chinese character. So here the C # truncation method will beIncorrectOf! Because the count in substring (startindex, count) is not the number of bytes, but the number of elements to be obtained (including Chinese characters and letters.

Therefore, the effective method of this question is implemented in C, which can implement the ASCII code of letters, that is, only one byte. (Omitted here)

 

 Appendix: number of bytes of different encoding in C #

In the same example, when different encodings are used, the number of bytes displayed is different. SeeCode:

// Description: specifies the number of bytes that are encoded and contain Chinese characters.

//Copyright:Http://www.cnblogs.com/yangmingming

// Notes: Two encoding methods are used.

Namespace Stringcutdemo
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
String Str1 =   " I am ABC " ;
String Str2 =   " Am I ABC? " ;
// The first encoding method:
Int Str1count = Encoding. Default. getbytecount (str1 );
Int Str2count = Encoding. Default. getbytecount (str2 );
// The second encoding method:
Int Str1unicodecount = Encoding. Unicode. getbytecount (str1 );
Int Str2unicodecount = Encoding. Unicode. getbytecount (str2 );
Console. writeline (
" The Byte Count of str1 and str2 is {0}, {1} " , Str1count, str2count );
Console. writeline (
" The byte by unicoding of str1 and str2 is {0}, {1} " , Str1unicodecount, str2unicodecount );
}

Debugging result

 

It can be seen that although the original ASCII single byte is stored in C # as two bytes, but because the second byte is 0, when different encodings are used, the number of bytes of the calculated string is different. In the first case, calculate the byte value as 1, and in the second case as 2.

 

Summary: it refers to the issue that the string containing Chinese characters is intercepted and extended to character encoding. Although there is little access to coding, This is a glimpse of the effect ~

 

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.