1

In C# I can do the following:

String1 = "Test \r\n test!";
String2 = @"Test \r\n test!";

output String1:

Test
 test!

output String2:

Test \r\n test!

In JavaScript I only found unescape(). But that is completly outdated and is not really what I was searching for, because that translated special characters to other things. I want that 'nothing' is translated, but everything is given out as it was in the string. Has someone an idea how I can do in JS what I can achieve in C# with the '@'?

deceze
  • 510,633
  • 85
  • 743
  • 889
Marcel Grüger
  • 885
  • 1
  • 9
  • 25
  • 4
    `JSON.stringify()` will effectively do what you want, or at least something pretty close to what you want. It'll encode things for the purpose of JSON syntax of course, but as I say that's close to what you describe. – Pointy Jun 25 '19 at 12:42
  • And yes `unescape()` is definitely not the tool for the job; it's for URL syntax. – Pointy Jun 25 '19 at 12:44
  • 1
    @Cid you're seeing the behavior of the console. Try `console.log(JSON.stringify(whatever))` – Pointy Jun 25 '19 at 12:45
  • @Pointy haha yes right my bad – Cid Jun 25 '19 at 12:45
  • @Pointy —- It won't if the input is `"\ "`. Escape sequences would be normalised by the initial parse. – Quentin Jun 25 '19 at 12:45
  • @Quentin yes, I realize that; that's why I qualified the recommendation. There's no way to retrieve the original "useless" escapes from the source string of course. – Pointy Jun 25 '19 at 12:48
  • Hm, ok. So I have two choices? Changing the string in advance, so that EVERY (even there are millions) backslash is changed to a double backslash OR I have work with the JSON variant where I have to deal with some extra " ? Hm, thanks. Not what I hoped for, but it is at least a hint. – Marcel Grüger Jun 25 '19 at 12:49
  • @MarcelGrüger it's basically just not a feature of the language. The pre-parse string constant text is not maintained anywhere at run time. – Pointy Jun 25 '19 at 12:50
  • 1
    @MarcelGrüger How have you managed to end up with JavaScript source code containing millions of incorrect string literals in the first place? – Quentin Jun 25 '19 at 12:54
  • Its meant for reading data from a database and that could be huge when it is some years in action. – Marcel Grüger Jun 25 '19 at 12:59
  • 1
    It sounds like you should be using a database for your data and not JS source code. – Quentin Jun 25 '19 at 13:00

1 Answers1

4

JavaScript has no equivalent to C# verbatim string literals.

You need to escape special characters when creating the string.

const string1 = "Test \\r\\n test!";

Some people have suggested using JSON.stringify, but the initial parsing of the string will normalise it, so you can't reliably recover the original input.

For example, an escaped space means the same as a space on its own.

const input = "A string containing a \ character";
const output = JSON.stringify(input);
document.write(output);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    I'm curious to know why this answer was downvoted – Cid Jun 25 '19 at 12:47
  • 1
    I am pretty sure initially it was because some people thought the comment about stringify answered it - now there are more to it than that – mplungjan Jun 25 '19 at 12:53