Can I format NULL values in string.Format?

前端 未结 5 1929
粉色の甜心
粉色の甜心 2021-01-01 08:55

I was wondering if there\'s a syntax for formatting NULL values in string.Format, such as what Excel uses

For example, using Excel I could specify a format value of

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 09:16

    I don't think there's anything in String.Format that will let you specify a particular format for null strings. A workaround is to use the null-coalescing operator, like this:

    const string DefaultValue = "(null)";
    
    string s = null;
    string formatted = String.Format("{0}", s ?? DefaultValue);
    

提交回复
热议问题