Escape Double Quotes in a String

Escape double quotes in a string

No.

Either use verbatim string literals as you have, or escape the " using backslash.

string test = "He said to me, \"Hello World\" . How are you?";

The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

Escape double quotes in a C# string

Just use @ for verbatim literal strings.

text.Replace(@"this", @"that");

Example:

text.Replace(@"\", @"\\").Replace(@"""", @"\""");

Don't escape double quotes in a string

If you want to to test your script with Java, with parameters containing quotes, you don't have any choice, you'll have to escape it.

String[] command  = new String[] {
"path/executeScript",
"--path=" + p,
"--user=" + user,
"--html=\"anchor bold\"",
"--port=8081"
}
Runtime.getRuntime().exec(command);

Technical explanation : https://stackoverflow.com/a/3034195/2003986

How do I replace a double-quote with an escape-char double-quote in a string using JavaScript?

You need to use a global regular expression for this. Try it this way:

str.replace(/"/g, '\\"');

Check out regex syntax and options for the replace function in Using Regular Expressions with JavaScript.

How to escape double quotes in JSON

Try this:

"maingame": {
"day1": {
"text1": "Tag 1",
"text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
}
}

(just one backslash (\) in front of quotes).

kdb: avoid escaping double quotation mark in string

Could you store the literal as a dict and convert to json at runtime? That way it remains clean and easier to read:

q)jstr:.j.j`key1`key2!("value1";"value2")
q)jstr~"{\"key1\":\"value1\",\"key2\":\"value2\"}"
1b

To answer your question - no, there's no way to avoid escaping in strings other than casting from some other form or running a lambda to generate the string. The built-in function .Q.s1 could help but I don't think an approach using that would be any better than the .j.j approach above

q).Q.s1"abc"
"\"abc\""

How to escape double quotes in Haskell?

You escape double quotes with a backslash (\" in a string), like:

"\"To be, or not to be\" - William Shakespeare"

The above can of course be rather cumbersome in case you need to write a lot of double quotes. Haskell enables quasiquotes a way that is used by many Haskell packages to develop "mini-languages" inside Haskell. Quasiquotes are to the best of my knowledge not specified in the Haskell report, so it is not really a "Haskell feature", but the most popular compiler (GHC) supports this. A package like raw-strings-qq [Hackage] allows you to make use of this feature to write raw strings, like this:

{-# LANGUAGE QuasiQuotes #-}

import Text.RawString.QQ(r)

quote = [r|"To be, or not to be" - William Shakespeare|]

this thus produces strings like:

Prelude Text.RawString.QQ> [r|"To be, or not to be" - William Shakespeare|]
"\"To be, or not to be\" - William Shakespeare"

QuasiQuotes are not only used to produce raw strings. In Yesod for example there are a few mini-languages to define HTML/CSS/JavaScript templates in Shakespearean languages (hamlet, lucius, cassius, julius). It is typically used if expressing something in "vanilla" Haskell would take a lot of work, whereas writing it in a specific language makes it easier.



Related Topics



Leave a reply



Submit