Using Quotation Marks Inside Quotation Marks

Using quotation marks inside quotation marks

You could do this in one of three ways:

  1. Use single and double quotes together:

    print('"A word that needs quotation marks"')
    "A word that needs quotation marks"
  2. Escape the double quotes within the string:

    print("\"A word that needs quotation marks\"")
    "A word that needs quotation marks"
  3. Use triple-quoted strings:

    print(""" "A word that needs quotation marks" """)
    "A word that needs quotation marks"

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)"#

Using quotation marks inside a SQL string

You just need to double the double quotes inside your SQL (that's how you escape double quotes in VB.NET).

Assuming the initial statement from your post is your working SQL:

& " : " & would become & "" : "" &,
and DateAdd("d",-7,Now()) would become DateAdd(""d"",-7,Now()).

The full statement:

Dim QuestionConnectionQuery As New OleDb.OleDbCommand("SELECT TOP 1 Questions.QuestionID, Questions.QuestionCategory & "" :  "" & Questions.QuestionSubCategory AS Category FROM Questions WHERE (((Questions.QuestionDifficulty)=[?])) OR (((Questions.LastDateRevealed) Is Null)) OR ((Questions.LastDateRevealed)>=DateAdd(""d"",-7,Now())) ORDER BY Rnd(QuestionID);", QuestionConnection)

How can I print a quotation mark in C?

Try this:

#include <stdio.h>

int main()
{
printf("Printing quotation mark \" ");
}

VB.Net Using Quote Mark in Quote Marks

Here's one option:

"text" & Chr(34) & textbox1.text & Chr(34)

RegEx: Grabbing values between quotation marks

I've been using the following with great success:

(["'])(?:(?=(\\?))\2.)*?\1

It supports nested quotes as well.

For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.



Related Topics



Leave a reply



Submit