When trying to include " in a String, using \" works but the \ is included too

80 Views Asked by At

I'm trying to send a string to an Http request using the following code

var outStr = "{product_code: \"660825\", quantity: \(qty)}"

As I've included the \ character it compiles ok but when I run it the value for outStr is

{product_code: \"660825\", quantity: 2}

What am I doing wrong and how can I lose the \?

1

There are 1 best solutions below

3
On BEST ANSWER

As you can see in the documentation:

String literals can include the following special characters:

The escaped special characters \0 (null character), \ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)

(snip)

let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein

you are doing it correctly, my guess would be that you server escapes the " as \" and that is why you are seeing {product_code: \"660825\", quantity: 2}.