How to Get Double Quotes into a String Literal

Can I escape a double quote in a verbatim string literal?

Use a duplicated double quote.

@"this ""word"" is escaped";

outputs:

this "word" is escaped

How can I get double quotes into a string literal?

Escape the quotes with backslashes:

printf("She said \"time flies like an arrow, but fruit flies like a banana\"."); 

There are special escape characters that you can use in string literals, and these are denoted with a leading backslash.

How to add double quotes in a string literal

If you want to include " in a string, supply "" where you want the quote to appear. So your example should read...

a = """1234,5678,9123"""

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.

How can I add double quotes to a string that is inside a variable?

You need to escape them by doubling them (verbatim string literal):

string str = @"""How to add doublequotes""";

Or with a normal string literal you escape them with a \:

string str = "\"How to add doublequotes\"";

How to print double quotes inside ?

With a backslash before the double quote you want to insert in the String:

let sentence = "They said \"It's okay\", didn't they?"

Now sentence is:

They said "It's okay", didn't they?

It's called "escaping" a character: you're using its literal value, it will not be interpreted.


With Swift 4 you can alternatively choose to use the """ delimiter for literal text where there's no need to escape:

let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""

This gives:

They said "It's okay", didn't they?

Yes, "okay" is what they said.


With Swift 5 you can use enhanced delimiters:

String literals can now be expressed using enhanced delimiters. A string literal with one or more number signs (#) before the opening quote treats backslashes and double-quote characters as literal unless they’re followed by the same number of number signs. Use enhanced delimiters to avoid cluttering string literals that contain many double-quote or backslash characters with extra escapes.

Your string now can be represented as:

let sentence = #"They said "It's okay", didn't they?"#

And if you want add variable to your string you should also add # after backslash:

let sentence = #"My "homepage" is \#(url)"#

How to add double quotes around a String variable in Java?

There are actually 2 issues with your code.

  1. You need to make sure that \" is a String. This can be done by surrounding it with double quotes like this: "\""

  2. You need to concatenate Strings when printing them by using a +

Change the last line of code in your question to:

System.out.println("\"" + upperCasePhrase + "\"");

How to quote \ (slash double-quote) in a string literal?

Try

string s = "\\\"\\\"";

You have to escape your backslashes too.

Mike

How to put data containing double-quotes in string variable?

You can escape (this is how this principle is called) the double quotes by prefixing them with another double quote. You can put them in a string as follows:

Dim MyVar as string = "some text ""hello"" hello"

This will give the MyVar variable a value of some text "hello" hello.



Related Topics



Leave a reply



Submit