Monday 19 June 2017

c# - How to add double quotes to a string that is inside a variable?

If you have to do this often and you would like this to be cleaner in code you might like to have an extension method for this.



This is really obvious code, but still I think it can be useful to grab and make you save time.



  /// 
/// Put a string between double quotes.

///

/// Value to be put between double quotes ex: foo
/// double quoted string ex: "foo"
public static string AddDoubleQuotes(this string value)
{
return "\"" + value + "\"";
}


Then you may call foo.AddDoubleQuotes() or "foo".AddDoubleQuotes(), on every string you like.




Hope this help.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...