我写了一个自定义的格式化程序,它实现了用于设置最大宽度的“L”格式说明符。当我们需要控制格式化输出的大小,比如发往数据库列或Dynamics CRM字段时,这非常有用。
public class StringFormatEx : IFormatProvider, ICustomFormatter
{
/// <summary>
/// ICustomFormatter member
/// </summary>
public string Format(string format, object argument, IFormatProvider formatProvider)
{
#region func-y town
Func<string, object, string> handleOtherFormats = (f, a) =>
{
var result = String.Empty;
if (a is IFormattable) { result = ((IFormattable)a).ToString(f, CultureInfo.CurrentCulture); }
else if (a != null) { result = a.ToString(); }
return result;
};
#endregion
//reality check.
if (format == null || argument == null) { return argument as string; }
//perform default formatting if arg is not a string.
if (argument.GetType() != typeof(string)) { return handleOtherFormats(format, argument); }
//get the format specifier.
var specifier = format.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture);
//perform extended formatting based on format specifier.
switch(specifier)
{
case "L":
return LengthFormatter(format, argument);
default:
return handleOtherFormats(format, argument);
}
}
/// <summary>
/// IFormatProvider member
/// </summary>
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
/// <summary>
/// Custom length formatter.
/// </summary>
private string LengthFormatter(string format, object argument)
{
//specifier requires length number.
if (format.Length == 1)
{
throw new FormatException(String.Format("The format of '{0}' is invalid; length is in the form of Ln where n is the maximum length of the resultant string.", format));
}
//get the length from the format string.
int length = int.MaxValue;
int.TryParse(format.Substring(1, format.Length - 1), out length);
//returned the argument with length applied.
return argument.ToString().Substring(0, length);
}
}
用法
var result = String.Format(
new StringFormatEx(),
"{0:L4} {1:L7}",
"Stack",
"Overflow");
Assert.AreEqual("Stac Overflo", result);
究竟是什么你想实现什么?你能发表你想看到什么样的例子吗? – Oded 2010-11-05 11:24:12
我想将C格式的字符串转换为C#格式的字符串。在C中,你可以指定%-4.4s和类似的。 – 2010-11-05 11:37:44
为什么投票?这是必不可少的问题! – 2010-11-05 12:04:26