GoldBishop
GoldBishop

Reputation: 2861

Format string if not null

I have a situation where I need to output, via CSV (Tab-delimited), a dataset of information.

Problem is that if the column value contains a value, then it needs to be double quoted.

The value types, could range from Alpha-Numeric strings to DateTime formatted values.

Was wondering if there was easier way than this:

(string.IsNullOrWhiteSpace(evt.Name)?null:string.Format("\"{0}\"", evt.Name))

for each value that is being exported to string value.

Edit 2013-07-08 11:06 CST (modified 11:17 CST)

public string QuoteFormat(object val)
{
    if(val != null) {
        if(val == typeof(string))
            val = (val as string).Replace("\"" , "\"\"");

        return string.Format("\"{0}\"" , val);
    }
    return null;
}

Upvotes: 2

Views: 3785

Answers (3)

Cemafor
Cemafor

Reputation: 1653

I made a small adjustment to the function you posted, if val is null, just return "". This explicity states that you want no charactors if val is 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 "";
    }
}

Upvotes: 1

terrybozzio
terrybozzio

Reputation: 4542

If you want to return null you can place a string null for letting you know the result was null:

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

Upvotes: 0

Simon Belanger
Simon Belanger

Reputation: 14870

You asked if there was a way to express your problem more clearly. While your own code is good and that we do not see a problem with it, I'll suggest you to use an extension.

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;
    }
}

And then use it with:

// Return "evt.Name" if String is not null or whitespaces
// return null otherwise
evt.Name.Transform(name => String.Format("\"{0}\"", name));

Or:

// Return ("evt.Name") if String is not null or whitespaces
// return (No Value) otherwise
evt.Name.Transform(name => String.Format("\"{0}\"", name), "No Value");

But as stated in the comments, you do not really need this as you code is good as it is.

Edit: for your own specific problem, you extension could be:

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();

Upvotes: 2

Related Questions