Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

c#

Is there some build in method that add quotes around string in c# ?

like image 884
Darqer Avatar asked Apr 21 '11 15:04

Darqer


People also ask

How do you escape quotation marks in a string?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do you put a double quote in a string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you handle double quotes in a string in C #?

To represent a double quotation mark in a string literal, use the escape sequence \". The single quotation mark (') can be represented without an escape sequence. The backslash (\) must be followed with a second backslash (\\) when it appears within a string.


2 Answers

string quotedString = string.Format("\"{0}\"", originalString); 
like image 26
Thomas Levesque Avatar answered Oct 06 '22 02:10

Thomas Levesque


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

like image 179
Jon Skeet Avatar answered Oct 06 '22 03:10

Jon Skeet