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"