C# 格式化字符串,但如果参数为空,则忽略该参数
我有一个生产代码的情况,请看下面的代码片段来可视化C# 格式化字符串,但如果参数为空,则忽略该参数,c#,C#,我有一个生产代码的情况,请看下面的代码片段来可视化 string formatStr = "{2} (FC {3:D3}): {1} {0}"; object obj= null; string str = string.Format(formatStr, "", "10 - 15","Blabla", obj); 这个输出将是“Blabla(FC):10-15” 请注意,没有与FC关联的值,因为obj为空,我不想打印FC,其中没有任何内容 所以我现在想的是检查obj并从中删除(FC{3:D3
string formatStr = "{2} (FC {3:D3}): {1} {0}";
object obj= null;
string str = string.Format(formatStr, "", "10 - 15","Blabla", obj);
这个输出将是“Blabla(FC):10-15”
请注意,没有与FC关联的值,因为obj为空,我不想打印FC,其中没有任何内容
所以我现在想的是检查obj并从中删除(FC{3:D3}):如果obj为null,是否有其他技术我不必这样做。我只有两个不同的格式字符串:
String str = String.Format(
obj == null ?
"{2}: {1} {0}" :
"{2} (FC {3:D3}): {1} {0}",
// 0 , 1, 2, 3
"", "10-15", "Blabla", obj
);
您可以在单独的步骤中构建
{3}
的值
string formatStr = "{2}{3}: {1} {0}";
object obj= null;
string fcValue = (obj == null ? "" : string.Format(" (FC {0:D3})", obj));
string str = string.Format(formatStr, "", "10 - 15", "Blabla", fcValue);
如果你是一个过度工程的粉丝,你可以实现一个。引入一种特殊的复合格式,将
字符串.format
与新的format和CustomFormatter实现一起输入,一切就绪
您的字符串格式如下所示:
string formatStr = "{2} {3:NULL:(FC {{0:D3}})}: {1} {0}";
object obj= null;
string str = string.Format(
new NullFormatter(), // use our custom formatter
formatStr, "", "10 - 15","Blabla", obj);
//https://msdn.microsoft.com/en-us/library/system.icustomformatter(v=vs.110).aspx
public class NullFormatter:ICustomFormatter, IFormatProvider
{
// IFormatProvider.GetFormat implementation.
public object GetFormat(Type formatType)
{
// Determine whether custom formatting object is requested.
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
// all formatting visits this
public string Format(String fmt, object obj, IFormatProvider fp)
{
// if obj = null always return an empty string
if (obj == null)
{
return String.Empty;
}
// do we have our special format?
if (fmt!= null && fmt.StartsWith("NULL:"))
{
// use the part after the NULL: as a formatstring
return String.Format(fmt.Substring(5), obj);
}
else
{
// no, do normal handling
if (obj is IFormattable) {
return ((IFormattable) obj).ToString(fmt, CultureInfo.CurrentCulture);
}
else
{
return obj.ToString();
}
}
return "?";
}
}
注意新的{3:NULL:(FC{{0:D3}}}
。customformatter将检查NULL:
字符串,然后使用(FC{0:D3})
作为该参数的格式字符串。需要双{{
和}
来转义{
和}
CustomFormatter如下所示:
string formatStr = "{2} {3:NULL:(FC {{0:D3}})}: {1} {0}";
object obj= null;
string str = string.Format(
new NullFormatter(), // use our custom formatter
formatStr, "", "10 - 15","Blabla", obj);
//https://msdn.microsoft.com/en-us/library/system.icustomformatter(v=vs.110).aspx
public class NullFormatter:ICustomFormatter, IFormatProvider
{
// IFormatProvider.GetFormat implementation.
public object GetFormat(Type formatType)
{
// Determine whether custom formatting object is requested.
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
// all formatting visits this
public string Format(String fmt, object obj, IFormatProvider fp)
{
// if obj = null always return an empty string
if (obj == null)
{
return String.Empty;
}
// do we have our special format?
if (fmt!= null && fmt.StartsWith("NULL:"))
{
// use the part after the NULL: as a formatstring
return String.Format(fmt.Substring(5), obj);
}
else
{
// no, do normal handling
if (obj is IFormattable) {
return ((IFormattable) obj).ToString(fmt, CultureInfo.CurrentCulture);
}
else
{
return obj.ToString();
}
}
return "?";
}
}
我不想过度设计@rene:)但是谢谢你的帮助,希望我能有足够的特权投票支持你。我会同意的,谢谢,