c#字符串自动补齐空格的问题
string str = "==";
int num = 30;
int left = (num - str.Length) / 2;
str = str.PadLeft(left, ' ').PadRight(num, ' ');
public static void Main()
{
string[] test = {"hello", "hi", "this is a test"};
test.ToList().ForEach(str => Console.WriteLine( ConvertStringTo30(str)));
Console.ReadKey();
}
public static string ConvertStringTo30(string src)
{
if(src.Length> 30)
throw new ArgumentOutOfRangeException("String is longer than 30");
var sb = new StringBuilder();
sb.Append('=', (30 - src.Length) / 2);
sb.Append(src);
sb.Append('=', 30 - sb.Length);
return sb.ToString();
}