how can i include a double quote in a string

R

Rick Tan

i need to a include a double quote in a string without it being escaped.
For example

astring="first few characters"
bstring="last characters"

When i tried
newstring = astring + '"' + bstring + '"'

newstring ended up as 'first few characters\"last characters\"'

What trick can i use to obtain a string 'first few characters "last
characters"' out of the 2 string variables. That is for ruby not to
escaped the double quote.

Thanks in advance.
 
N

Nathan Clark

i need to a include a double quote in a string without it being escaped.
For example

astring=3D"first few characters"
bstring=3D"last characters"

When i tried
=C2=A0 newstring =3D astring + '"' + bstring + '"'

newstring ended up as 'first few characters\"last characters\"'

What trick can i use to obtain a string 'first few characters "last
characters"' out of the 2 string variables. =C2=A0That is for ruby not to
escaped the double quote.

Thanks in advance.

irb> newstring
=3D> "first few characters\"last characters\""
irb> puts newstring
first few characters"last characters"
=3D> nil

The quotation marks aren't actually escaped, they're just shown that
way on inspection to differentiate them from the quotation marks
delimiting the entire string.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

i need to a include a double quote in a string without it being escaped.
For example

astring="first few characters"
bstring="last characters"

When i tried
newstring = astring + '"' + bstring + '"'

newstring ended up as 'first few characters\"last characters\"'

What trick can i use to obtain a string 'first few characters "last
characters"' out of the 2 string variables. That is for ruby not to
escaped the double quote.

Thanks in advance.
I assume you are doing this in irb, and looking at the result. If that is
the case, irb inspects whatever it receves, since it receives a string with
quotes in it, to display a string, it wraps the string in quotes. Thus it
has to differentiate the internal quotes from the external, and escapes them
before displaying the result to you. However, they are not _actually_
escaped, only displayed that way by some types of displaying. Try printing
it out and you will see. Here are five ways to do this, I think either the
first one, or %Q are the best, because they are easiest to read / most
straightforward..


a = "first"
b = "last"

[ "\"#{a}\" #{b}" ,
'"' + a + '" ' + b,
'"' << a << '" ' << b,
%Q("#{a}" #{b}),
<<-ENDOFSTRING.strip,
"#{a}" #{b}
ENDOFSTRING
].each do |str|
puts "normal: #{str}"
puts "inspected: #{str.inspect}"
puts
end
 
B

Brian Candler

Rick Tan wrote in post #971378:
i need to a include a double quote in a string without it being escaped.
For example

astring="first few characters"
bstring="last characters"

When i tried
newstring = astring + '"' + bstring + '"'

newstring ended up as 'first few characters\"last characters\"'

$ irb --simple-promptfoo"bar << NOTE
=> nil7 << NOTE
=> nil

So the string is 7 characters: f o o " b a r

irb uses #inspect to display the value of each expression you type.
String#inspect escapes double-quotes, so that the value is a valid
string literal (i.e. could be pasted back in to Ruby)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top