Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Display A String With Double Quotes

DZone's Guide to

How To Display A String With Double Quotes

·
Free Resource
To escape quotes in a string %q (single quotes) and %Q (double quotes) are typically used e.g.

testing = "testing with \"quotes\""
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %q(testing with "quotes")
puts testing      #=> testing with "quotes"

testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q(testing with "quotes")
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %q[testing with "quotes"]
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q[testing with "quotes"]
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""

testing = %Q*testing with "quotes"*
puts testing      #=> testing with "quotes"
testing.inspect   #=> "\"testing with \\\"quotes\\\"\""


# -- with nested ruby code ---------

testing = "testing #{Time.now} with \"quotes\""
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %q(testing #{Time.now} with "quotes")
puts testing     #=> testing #{Time.now} with "quotes"
testing.inspect  #=> "\"testing \\\#{Time.now} with \\\"quotes\\\"\""

testing = %Q(testing #{Time.now} with "quotes")
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %q[testing #{Time.now} with "quotes"]
puts testing     #=> testing #{Time.now} with "quotes"
testing.inspect  #=> "\"testing \\\#{Time.now} with \\\"quotes\\\"\""

testing = %Q[testing #{Time.now} with "quotes"]
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""

testing = %Q*testing #{Time.now} with "quotes"*
puts testing     #=> "testing 2010-04-03 10:25:14 +0100 with "quotes"
testing.inspect  #=> "\"testing 2010-04-03 10:25:14 +0100 with \\\"quotes\\\"\""


Resources:
 - Ruby Programming/Alternate quotes - Wikibooks, collection of open-content textbooks [wikibooks.org]
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}