2

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;
}
15
  • 5
    Not really, that's pretty simple. Commented Jul 8, 2013 at 15:52
  • 1
    @SamLeach wasn't sure if there was a LINQ format that gets same action done or not. I try to keep it simple to execute and read for after-me developers. Commented Jul 8, 2013 at 15:53
  • 3
    @GoldBishop Linq works well with collections and everything. But in this case, you do not want to work with a collection of char. LINQ is nice and fun, but it's not the best tool all the time. Commented Jul 8, 2013 at 15:55
  • 2
    @GoldBishop Your edit will throw an error when val is null in: return val.ToString() Commented Jul 8, 2013 at 16:08
  • 1
    @GoldBishop I feel you should make use of method overloading. You could have a QuoteFormat(string val) living alongside of QuoteFormat(object val). That will remove the need to check for the type. Commented Jul 8, 2013 at 18:17

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

good call. If I find myself doing this against outside of the exporter, I will definitely extract the pattern up a level so its available to all locations, for use.
I would push the format into the extention itself, just to reduse the amout of copied code if the same Transform needs to be done in several places. Or I would at least use name inside the lambda as well String.Format("\"{0}\"", name)
@Cemafor evt.Name in the lambda was a typo. Fixing it. ;)
1

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

Comments

0

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 -> ""

5 Comments

nah, if its null then I need the tab characters to be next to each other in the file \t\t, for instance.
can you provide an string example of what you really mean for example the result should be if not null "something" or \something\,and null should be "" or something else?
Doubt it will show up correctly but here is a partial set of data with mix types: "6/4/2014 10:00:00 PM" 15 0 "active" "Webinar"
but is that string evt.name? or evt.name is a part of that string like the part "active",meaning evt.name has to be inserted into that string or the is it the whole string evt.name?
its not a nullable value from evt.name but strings dont need to be nullable as they are inherintly nullable by nature of their type. Null for strings is nothing more than 0 length string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.