The C # language's method of "mixed string alignment in English and Chinese"

Source: Internet
Author: User

Reference from: (1) http://www.cnblogs.com/cnluoke/articles/1213398.html

(2) http://www.cnblogs.com/sql4me/archive/2009/11/16/1603996.html

Method One: Populate an existing string when it is organized into a new string

Because of a field, it contains Chinese characters and numbers. And a Chinese character is the size of a two-digit placeholder, and if you look at the length of a string, the length of a single character is 1. A Chinese character is equal to a placeholder (obviously wrong) if it is calculated by a string containing the length of the character string equal to the placeholder size (if it contains only letters and numbers), so there is a problem and the field cannot be aligned. Because alignment is based on the placeholder size.

For example: Name field, String Name= "Chinese 123"; So name.length=6. If with encoding.getencoding ("gb2312"). GetBytes (name). Length=9. Assume that the name field specifies a size of 12, and that less than the available spaces for the string are populated. The Fill Name field has a length of 12, which requires an additional 3 empty glyd to fill the field instead of 6 spaces.

Workaround: String name=name+new string (' ', 12-encoding.getencoding ("gb2312"). GetBytes (name). Length);

==============================================================

Method Two: Extend String.PadLeft and String.padright

private string Padleftex (string Str,int totalbytecount,char c)
{
Encoding coding = encoding.getencoding ("gb2312");
int dcount = 0;
foreach (char ch in str.) ToCharArray ())
{
if (coding. GetByteCount (Ch. ToString ()) = = 2)
dcount++;
}
string w = str. PadRight (TOTALBYTECOUNT-DCOUNT,C);
Return w;
}

private string Padrightex (string Str,int totalbytecount,char c)
{
Encoding coding = encoding.getencoding ("gb2312");
int dcount = 0;
foreach (char ch in str.) ToCharArray ())
{
if (coding. GetByteCount (Ch. ToString ()) = = 2)
dcount++;
}
string w = str. PadRight (TOTALBYTECOUNT-DCOUNT,C);
Return w;
}

The C # language's method of "mixed string alignment in English and Chinese"

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.