Auto quotes around string in c# – build in method?

Do you mean just adding quotes? Like this?

text = "\"" + text + "\"";

? I don’t know of a built-in method to do that, but it would be easy to write one if you wanted to:

public static string SurroundWithDoubleQuotes(this string text)
{
    return SurroundWith(text, "\"");
}

public static string SurroundWith(this string text, string ends)
{
    return ends + text + ends;
}

That way it’s a little more general:

text = text.SurroundWithDoubleQuotes();

or

text = text.SurroundWith("'"); // For single quotes

I can’t say I’ve needed to do this often enough to make it worth having a method though…

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)