-2

Below is my source data.

string s = "text"some text in quotes next text.";

when i try to assign, it's throwing an error.

when i use this \ escape character before ", it is working fine.

string s = "text\"some text in quotes\" next text.";

but my client will send data like below only

text"some text in quotes next text.

how can I assign the above value to string?

I have tried using like below, but throwing error.

string a1 = @"text"some text in quotes next text.";
MRebai
  • 5,344
  • 3
  • 33
  • 52
user2979719
  • 59
  • 1
  • 1
  • 8
  • 1
    Define "my client will send data". What does that mean? If you're receiving this value in the form of a variable then where does a literal string even come into the picture here? – David Jul 03 '14 at 12:36
  • 1
    _"my client will send data"_ Then you don't have the problem, have you? If this is just for testing, test it under real conditions and use a file (or whatever it is). – Tim Schmelter Jul 03 '14 at 12:36
  • 3
    This makes no real sense. You already know how to escape quotes. What is the problem? – David Heffernan Jul 03 '14 at 12:36
  • What is the incoming data? Escaping of specific characters in your code has nothing to do with the same characters inputted through console, textbox, read from XML, whatever - http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma. They will be in the string as they are being inputted - no escaping and replacing will be done. – Eugene Podskal Jul 03 '14 at 12:39

1 Answers1

1

Escape your double quotes:

 string s = "text\"some text in quotes next text.";

Alternative:

 string s = @"text""some text in quotes next text.";

So if you receive the string, or parse it from xml, look up the double quotes and escape them as described above before assigning it to a string variable, like:

 string s = xmlValue.Replace('"', '\"');
L-Four
  • 13,345
  • 9
  • 65
  • 109