how can i include a double quote in a string

Discussion in 'Ruby' started by Rick Tan, Dec 30, 2010.

  1. Rick Tan

    Rick Tan Guest

    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/.
     
    Rick Tan, Dec 30, 2010
    #1
    1. Advertising

  2. Rick Tan

    Nathan Clark Guest

    > 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.
     
    Nathan Clark, Dec 30, 2010
    #2
    1. Advertising

  3. Rick Tan

    Josh Cheek Guest

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

    On Wed, Dec 29, 2010 at 6:31 PM, Rick Tan <> 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
     
    Josh Cheek, Dec 30, 2010
    #3
  4. 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/.
     
    Brian Candler, Dec 30, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    3
    Views:
    13,261
  3. Eric Layman
    Replies:
    3
    Views:
    635
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Evan
    Replies:
    3
    Views:
    525
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. vikrant
    Replies:
    8
    Views:
    366
    vikrant
    May 17, 2007
Loading...

Share This Page