//可以对齐
Console.WriteLine("{0,-20}{1}", "fafdaafd奔号:", "213");
Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
//增加一个英文字母t,可以对齐
Console.WriteLine("{0,-20}{1}", "fafdatafd奔号:", "213");
Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
//增加一个汉字减少两个字母,可以对齐
Console.WriteLine("{0,-20}{1}", "fafdfd我奔号:", "213");
Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
//增加一个汉字减少一个字母,不可以对齐
Console.WriteLine("{0,-20}{1}", "fafdaafd我奔号:", "213");
Console.WriteLine("{0,-20}{1}", "fdfaa奔号:", "13");
以下为控制台输出的结果
以下为消息框的显示:
处理方法 :
public class QString
{
string s;
/// <summary>
/// 新建实例
/// </summary>
/// <param name="s"></param>
public QString(string s)
{
this.s = s;
}
/// <summary>
/// 待格式化的
/// </summary>
/// <param name="length">长度</param>
/// <param name="chr">补码</param>
/// <returns>格式化的数据</returns>
/// <remarks>
/// 以英文字母为基准,根据最大长度与实际长度计算补码长度
/// 如:c 20
/// c我 20-1
/// c多我我 20-3
/// </remarks>
public string PadLeft( int length, char chr)
{
return s.PadLeft(length - new System.Text.RegularExpressions.Regex(@"[\u4e00-\u9fa5]").Matches(s).Count, chr);
}
/// <summary>
/// 待格式化的
/// </summary>
/// <param name="length">长度</param>
/// <param name="chr">补码</param>
/// <returns>格式化的数据</returns>
/// <remarks>
/// 以英文字母为基准,根据最大长度与实际长度计算补码长度
/// 如:c 20
/// c我 20-1
/// c多我我 20-3
/// </remarks>
public string PadRight(int length, char chr)
{
return s.PadRight(length - new System.Text.RegularExpressions.Regex(@"[\u4e00-\u9fa5]").Matches(s).Count, chr);
}
}
2009-10-29
经过大量实践,能在比较多的场合使用,但MSGBOX不适用。
using System;
using System.Collections.Generic;
using System.Text;
namespace QouShui.DLL
{
public class QString
{
string s;
/// <summary>
/// 新建实例
/// </summary>
/// <param name="s"></param>
public QString(string s)
{
this.s = s;
hcount = reg.Matches(s).Count ;
}
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"[\uFF00-\uFFFF\u4e00-\u9fa5]");
int hcount = 0;
/// <summary>
/// 待格式化的
/// </summary>
/// <param name="length">长度</param>
/// <param name="chr">补码</param>
/// <returns>格式化的数据</returns>
/// <remarks>
/// 以英文字母为基准,根据最大长度与实际长度计算补码长度
/// 如:c 20
/// c我 20-1
/// c多我我 20-3
/// </remarks>
public string PadLeft(int length, char chr)
{
if (length - s.Length - reg.Matches(s).Count < 0)
length = s.Length + reg.Matches(s).Count;
return s + new string(chr, length - s.Length - reg.Matches(s).Count);
}
/// <summary>
/// 待格式化的
/// </summary>
/// <param name="length">长度</param>
/// <param name="chr">补码</param>
/// <returns>格式化的数据</returns>
/// <remarks>
/// 以英文字母为基准,根据最大长度与实际长度计算补码长度
/// 如:c 20
/// c我 20-1
/// c多我我 20-3
/// </remarks>
public string PadRight(int length, char chr)
{
if (length - s.Length - reg.Matches(s).Count < 0)
length = s.Length + reg.Matches(s).Count;
return new string(chr, length - s.Length - reg.Matches(s).Count) + s ;
}
}
}