using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace PrintDemo { public class FileUtils { public static string getFixedWidthString(string strinput,int lenght) { return StringExtensions.PadRightWhileDouble(strinput, lenght, ' '); } } public static class StringExtensions { /// /// 按单字节字符串向左填充长度 /// /// /// /// /// public static string PadLeftWhileDouble(this string input, int length, char paddingChar = '\0') { var singleLength = GetSingleLength(input); return input.PadLeft(length - singleLength + input.Length, paddingChar); } private static int GetSingleLength(string input) { if (string.IsNullOrEmpty(input)) { return 0; } return Regex.Replace(input, @"[^\x00-\xff]", "aa").Length;//计算得到该字符串对应单字节字符串的长度 } /// /// 按单字节字符串向右填充长度 /// /// /// /// /// public static string PadRightWhileDouble(this string input, int length, char paddingChar = '\0') { var singleLength = GetSingleLength(input); return input.PadRight(length - singleLength + input.Length, paddingChar); } } }