4

I have the following raw-string literal : @"some text ""here"" and some more text" Everything works fine when I have this assigned to a string variable in a program.
But when I put this string - "some text ""here"" and some more text" and read it, it is not being recognized as a raw string.

What should I do to have C# recognize this as a raw-string? Specifically, what is the programmatic equivalent of the '@' specifier used to indicate that the string is a raw-string?

Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
  • How do you save it and read it? Do you save in text file, xml...? – Sebastian Siek Feb 09 '12 at 13:16
  • would you care to show your code how you do int presently? otherwise you add there escape character for every quote you have – cpoDesign Feb 09 '12 at 13:18
  • A string is a string - if it contains quotes or not. What exactly is the problem you are facing with these strings? – Oded Feb 09 '12 at 13:20
  • Hi, the text is in a .txt file which I dont own..I can only read the file.. – Aadith Ramia Feb 09 '12 at 13:22
  • OK. And what exactly is the problem in reading the file? If the text contains quotes, the string you get will contain quotes. – Oded Feb 09 '12 at 13:24
  • 1
    You're confused here. the @ sign is just a way of telling the C# compiler to threat a couple of double quotes as a literal double quote rather than an end- and opening quote for a string (since strings in C# need to reside between double quotes. That's just how the language goes. From a programmatic point of view, there is no need to implement a solution for double quotes (depending on the context), when you put the following text in a text file: "hello"world, and you read the whole bunch in a string using a StreamReader, the double quotes will be interpreted as a literal. – Polity Feb 09 '12 at 13:25
  • @Oded the example provided in problem statement is a hypothetical one.. the actual string in file is JSON. the keys are of the form ""key""..after I read the contents of the file, I am not able to deserialize the text back to JSON object – Aadith Ramia Feb 09 '12 at 13:26
  • 1
    @Aadith - Why don't you ask about that instead of going into a discussion about literal strings and such? If your file is as defined, it is **not** valid JSON. – Oded Feb 09 '12 at 13:26
  • @Oded When I copy th string from file and put it in a program as @"string" , I am able to deserialize it..so the text in file is a valid JSON – Aadith Ramia Feb 09 '12 at 13:29
  • Can you please explain `When I copy th string from file and put it in a program as @"string"`? How do you do that? – Oded Feb 09 '12 at 13:40

3 Answers3

2

why dont you escape it ?

"some text \"here\" and some more text"

enter image description here

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1

There's no programmatic equivalent of the @ specifier. The specifier is used only at compile time to convert a verbatim string literal to its internal representation. (BTW, the proper name is verbatim string, not raw-string).

Therefore, str1 and str2 below represent exactly the same string at runtime:

string str1 = "some text \"here\" and some more text";
string str2 = @"some text ""here"" and some more text";

The one and only difference between them is visible in your source code only. You don't need to apply any kind of programmatic transformation when your read strings from a text file.

Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
0

I think when you say raw string, you mean literal string.

http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

Literal strings using the @ symbol are just telling the compiler to literally take the text provided.

As far as I know there is no way to convert a string into the literal version of that string.

But you could easily write that code that replaces double quotes with single quotes.

madmik3
  • 6,975
  • 3
  • 38
  • 60