-1

I have a javascript code that is given as

var info = document.getElementsByTagName("info");

Now I would like to get this js as string in Unity (as I am performing injection) and I am able to do it as

string information = "var info = document.getElementsByTagName('info');"

As you see I have to change the quote to single quote here. I do not want to do this. How do I avoid it and use the string as I get it? Unfortunately, I have t

Sana
  • 15
  • 4
  • You can escape the double quotes with the backslash character, so `string information = "var info = document.getElementsByTagName(\"info\");";` – oRoiDev Dec 09 '21 at 16:25
  • Yes but is there a way to avoid the backslash? I do not want to alter the string in any way. – Sana Dec 10 '21 at 08:20

1 Answers1

1

You need to escape the double quotes. You can do this using the backslash character:

string information = "var info = document.getElementsByTagName(\"info\");";

The extra \ characters are interpreted by the compiler and are never part of the actual string value. The strings value will not contain the backslashes. There is no way of expressing a string that contains quotes without escaping them.

Sean McCafferty
  • 226
  • 2
  • 5
  • Thanks but is there a way to avoid the backslash? I do not want to alter the string in any way. – Sana Dec 10 '21 at 08:20
  • 1
    @Sana You're misunderstanding escaping. The extra \ characters are interpreted by the compiler and are never part of the actual string value. The strings value will not contain the backslashes. There is no way of expressing a string that contains quotes without escaping them. If my answer has answered your question could you please mark it as your accepted answer? Thanks – Sean McCafferty Dec 10 '21 at 15:06
  • @SeanMcCafferty your comment in your answer could make it into part of the answer, it would improve it – Cleptus Dec 10 '21 at 20:28
  • @Cleptus I've updated my answer. Thanks for the feedback! – Sean McCafferty Dec 15 '21 at 14:43