Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how can i include a double quote in a string

Reply
Thread Tools

how can i include a double quote in a string

 
 
Rick Tan
Guest
Posts: n/a
 
      12-30-2010
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Nathan Clark
Guest
Posts: n/a
 
      12-30-2010
> 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.

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      12-30-2010
[Note: parts of this message were removed to make it a legal post.]

On Wed, Dec 29, 2010 at 6:31 PM, Rick Tan <(E-Mail Removed)> wrote:

> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      12-30-2010
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-prompt
>> a = "foo" + '"' + "bar"

=> "foo\"bar"
>> puts a

foo"bar << NOTE
=> nil
>> puts a.size

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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem with single-quote and double-quote when using subprocess. Evan Python 3 11-04-2008 12:24 PM
How to handling string contains single quote and double quote vikrant Perl Misc 8 05-17-2007 04:37 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Single Quote Versus Double Quote In A href link knee-dragger@hotmail.com HTML 3 06-13-2006 12:42 AM



Advertisments