using System.Collections.Generic;
public static void Main()
var strDiff = "詹éʉ小壡O?abcD";
Console.WriteLine("{0}{1}", padRightEx(strDiff, 10), "end");
Console.WriteLine("{0}{1}", padRightEx_bak(strDiff, 10, strEncode), "end");
private static string padRightEx(string str, int totalByteCount )
StringBuilder sbResult = new StringBuilder();
coding = CodePagesEncodingProvider.Instance.GetEncoding("big5");
foreach (char ch in str.ToCharArray())
currChar = ch.ToString();
big5Char = coding.GetString(coding.GetBytes(currChar));
ascii = Convert.ToInt32(ch);
if ( big5Char == "?" || big5Char == "?" || ascii == 32 || big5Char == "?") {
currChByte = coding.GetByteCount(currChar);
if ( ( intCurrentByte + currChByte ) <= totalByteCount ) {
intCurrentByte = intCurrentByte + currChByte;
sbResult.Append(currChar);
Console.WriteLine("PadRight:" + sbResult.ToString().PadRight( totalByteCount - intBig5Cnt ) + "end");
Console.WriteLine(" PadLeft:" + sbResult.ToString().PadLeft( totalByteCount - intBig5Cnt) + "end");
return sbResult.ToString().PadRight( totalByteCount - intBig5Cnt );
private static string padRightEx_bak(string str, int totalByteCount, string strEncode )
Encoding codeBig5 = CodePagesEncodingProvider.Instance.GetEncoding("big5");
if ( strEncode.ToUpper() == "BIG5" )
coding = CodePagesEncodingProvider.Instance.GetEncoding("big5");
coding = Encoding.Default;
foreach (char ch in str.ToCharArray())
var intChByte = coding.GetByteCount(ch.ToString());
if ( ( intCurrentByte + intChByte - intBig5Cnt - 1 ) > totalByteCount )
intCurrentByte = intCurrentByte + intChByte;
var byteArray = coding.GetBytes(str).Take( intCurrentByte ).ToList();
Console.WriteLine("PadRight:" + coding.GetString(byteArray.ToArray()).PadRight( totalByteCount - intBig5Cnt ) + "end");
Console.WriteLine(" PadLeft:" + coding.GetString(byteArray.ToArray()).PadLeft( totalByteCount - intBig5Cnt ) + "end");
return coding.GetString(byteArray.ToArray()).PadRight( totalByteCount - intBig5Cnt );