Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果不为空,则格式化字符串_C#_String Formatting - Fatal编程技术网

C# 如果不为空,则格式化字符串

C# 如果不为空,则格式化字符串,c#,string-formatting,C#,String Formatting,我需要通过CSV(制表符分隔)输出一个信息数据集 问题是,如果列值包含一个值,那么它需要双引号 值类型的范围从字母数字字符串到日期时间格式的值 我想知道是否有比这更简单的方法: (string.IsNullOrWhiteSpace(evt.Name)?null:string.Format("\"{0}\"", evt.Name)) 对于要导出为字符串值的每个值 编辑2013-07-08 11:06 CST(修改为11:17 CST) 你问是否有办法更清楚

我需要通过CSV(制表符分隔)输出一个信息数据集

问题是,如果列值包含一个值,那么它需要双引号

值类型的范围从字母数字字符串到日期时间格式的值

我想知道是否有比这更简单的方法:

(string.IsNullOrWhiteSpace(evt.Name)?null:string.Format("\"{0}\"", evt.Name))
对于要导出为字符串值的每个值

编辑2013-07-08 11:06 CST(修改为11:17 CST)
你问是否有办法更清楚地表达你的问题。虽然您自己的代码很好,而且我们认为它没有问题,但我建议您使用扩展

public static class GoldBishopExtensions
{
    public static string Transform(this string source, Func<string, string> transform, string fallback = null)
    {
        return !String.IsNullOrWhiteSpace(source) 
                   ? transform(source) 
                   : fallback;
    }
}
或:

但是正如在评论中所说的,您并不真正需要它,因为您的代码是好的

编辑:对于您自己的特定问题,您可以:

public static class GoldBishopExtensions
{
    public static string Quote(this string source, string fallback = null)
    {
        return !String.IsNullOrWhiteSpace(source) 
                   ? String.Format("\"{0}\"", source) 
                   : fallback;
    }
}

evt.Name.Quote();

如果要返回null,可以放置一个字符串null,让您知道结果为null:

string result = String.Format("\"{0}\"", evt.Name ??"null");//output -> "null"
//string result = String.Format("\"{0}\"", evt.Name ??null);// output -> ""

我对您发布的函数做了一个小的调整,如果
val
null
,只需返回
。此明确说明,如果
val
null
,则不需要字符

public string QuoteFormat(object val)
{
    if(val != null) {
        // This is fine if there is an additional requirement to double existing quotes
        if(val == typeof(string))
            val = (val as string).Replace("\"" , "\"\"");

        // if you are using dates and the default format is fine, then this will work.
        // otherwise I would suggest formatting the date before passing in and perhaps
        // changing the parameter to string
        return string.Format("\"{0}\"" , val);
    } else {
        return "";
    }
}

不太好,这很简单。@SamLeach不确定是否有LINQ格式可以完成相同的操作。我尽量使它易于执行,并为后继开发人员阅读。@GoldBishop Linq能很好地处理集合和所有内容。但在本例中,您不希望使用
char
的集合。LINQ很好,也很有趣,但它不是一直以来最好的工具。@GoldBishop如果
val
中的
return val.ToString()
@GoldBishop中的值为空,则您的编辑将抛出一个错误。我觉得您应该使用方法重载。您可以有一个
QuoteFormat(string val)
QuoteFormat(object val)
一起使用。这样就不需要检查类型。很好。如果我发现自己在导出器之外这样做,我肯定会将模式提取到一个级别,以便所有位置都可以使用。我会将格式推入扩展本身,如果需要在多个位置进行相同的转换,只需减少复制的代码数量。或者我至少会在lambda中使用
name
以及
String.Format(“{0}\”,name)
@cemaforevt。lambda中的name是一个打字错误。修好它不,如果为空,那么我需要制表符在文件
\t\t
中相邻。您能提供一个字符串示例说明您真正的意思吗?例如,如果不是空,结果应该是“某物”或“某物”,而空值应该是“”或者其他什么?怀疑它是否会正确显示,但这里有一组混合类型的部分数据:
“6/4/2014 10:00:00 PM”15 0“活动”“网络研讨会”
,但这是字符串evt.name吗?或者evt.name是该字符串的一部分,就像“active”部分一样,这意味着必须将evt.name插入该字符串中,或者是整个字符串evt.name?它不是
evt.name
中的可空值,但是字符串不需要为空,因为它们的类型本质上是可以为空的。字符串的Null长度不超过0个字符串。
public static class GoldBishopExtensions
{
    public static string Quote(this string source, string fallback = null)
    {
        return !String.IsNullOrWhiteSpace(source) 
                   ? String.Format("\"{0}\"", source) 
                   : fallback;
    }
}

evt.Name.Quote();
string result = String.Format("\"{0}\"", evt.Name ??"null");//output -> "null"
//string result = String.Format("\"{0}\"", evt.Name ??null);// output -> ""
public string QuoteFormat(object val)
{
    if(val != null) {
        // This is fine if there is an additional requirement to double existing quotes
        if(val == typeof(string))
            val = (val as string).Replace("\"" , "\"\"");

        // if you are using dates and the default format is fine, then this will work.
        // otherwise I would suggest formatting the date before passing in and perhaps
        // changing the parameter to string
        return string.Format("\"{0}\"" , val);
    } else {
        return "";
    }
}