-2

What is the best way to return this string in C#?

    "<img src="${matches[1]}" style="height: auto; width: 100%; max-width: 100%;">";

That string is in JavaScript.

${matches[1]} is a variable that I have, but I'm but sure what do with the quotation marks, Semi-colon, or the angle bracket on the right.

Any help is appreciated!

Momoro
  • 599
  • 4
  • 23
nik
  • 514
  • 2
  • 6
  • 19
  • Not really sure what the question is.. You want to return that string in **C#**? You need to add a backslash before any special chars, e.g. **\x0** or **\"** – Momoro Apr 29 '20 at 21:18

2 Answers2

3

Don't want to talk whether it's a good idea to have such a string hardcoded in your code. Just to answer your question:

There is a defined list of literals that need to be escaped when you want to use it inside a string in your code. The brackets and also the semicolon is not part of that list, so you can safely use them without any modifications. When you want to use the doublequote character, you have to escape it with the backslash. So it becomes like

var helloWorld = "This is \"my\" world";

More about all characters that need to be escaped can be found here for example: Can I convert a C# string value to an escaped string literal

Another option could be the use of the verbatim identifier @. Read more about that here

That can be useful if you have strings with the backslash inside, for example when dealing with paths.

var myPath1 = "C:\\Windows\\System32";
var myPath2 = @"C:\Windows\System32";

With the @ there is no need to escape the backslash. But then the doublequote character still needs escapement, but this time just by writing it twice:

var helloWorld = @"This is ""my"" world";

You can decide what fits best to your coding style and your actual requirements.

var myString1 = "<img src=\"${matches[1]}\" style=\"height: auto; width: 100%; max-width: 100%;\">";

or

var myString2 = @"<img src=""${matches[1]}"" style=""height: auto; width: 100%; max-width: 100%;"">";
Torben Schramme
  • 2,104
  • 1
  • 16
  • 28
1
var mystring =  "<img src=\"${matches[1]}\" style=\"height: auto; width: 100%; max-width: 100%;\">";
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794