Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express a string literal containing double quotes without escape characters?

Tags:

c#

Is there a C# syntax with which I can express strings containing double quotes without having to escape them? I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.

Eg. presently for the following string (simple example)

"No," he said.

I write in C# "\"No,\" he said."

But I'd rather write something like Python '"No," he said.', or Ruby %q{"No," he said.}, so I can copy and paste the contents verbatim to other apps.

like image 668
Colonel Panic Avatar asked Jan 15 '13 13:01

Colonel Panic


People also ask

How do you write a double quote as a string literal?

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.

Are character literals enclosed in double quotes?

Character literals are enclosed in single quotation marks.

How do you fix string literal is not properly closed by a double quote?

It (more or less) means "treat the next character as special" - so here java thinks you are trying to put a double quote inside a quoted string. sometimes you need to do that. To get around this, you have to tell java to treat the backslash in a special way. so, you need TWO backslashes.


1 Answers

I frequently copy and paste strings between C# source code to other apps, and it's frustrating to keep adding and removing backslashes.

Then it sounds like you probably shouldn't have the strings within source code.

Instead, create text files which are embedded in your assembly, and load them dynamically... or create resource files so you can look up strings by key.

There's no form of string literal in C# which would allow you to express a double-quote as just a single double-quote character in source code.

like image 71
Jon Skeet Avatar answered Oct 23 '22 12:10

Jon Skeet