日期:2014-05-17 浏览次数:21382 次
//每行最多30个字符,不够30个的用空格补齐(前后补齐对称,居中显示).效果如下:
================================
============================
========================
====================
================
============
========
====
==
=
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) /