我收到一个消息和一个Priority
枚举并返回一个格式化的字符串的方法。对齐字符串string.Format()
private string FormatMessage(string message, Priority priority)
{
return string.Format("*{0,-6}* - {1}", priority, message);
}
Priority
有三个可能的值:High
,Medium
和Low
。
我使用string.Format
的对齐选项,以便输出看起来不错。什么想我输出的样子是这样的:
*Low* - First message
*Medium* - Second message
*Low* - Third message
然而,什么我得到的是:
*Low * - First message
*Medium* - Second message
*Low * - Third message
我明白为什么会这样,但想什么,我要知道是否有一种简单(并且正确)的方式来通过使用string.Format
并且不引入任何新变量来获得想要的输出。
为什么不直接将串联“*”+优先级+“*”' – musefan 2012-03-15 08:41:46