Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Double quote escape character

Reply
Thread Tools

Double quote escape character

 
 
Alvaro Perez
Guest
Posts: n/a
 
      08-16-2007
Hi all,

I'm trying to export a string to a excel file using fastercsv and I
wanted it double quote to make it safer. So i tried to use the escape
character \" to insert double quotes but it doesn't work:

- string produces string with no quotes in the file

- "\"" << string << "\"" produces """string"""

- "\"" + string + "\"" produces """string"""

I have no idea on how to solve this.


Thanks for the help,
Alvaro.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Alvaro Perez
Guest
Posts: n/a
 
      08-16-2007
Hi all,

I think there is not problem at all as it seems that the FasterCSV class
is the one who's erasing the double quotes while converting the strings
to cvs.

Anyway, it's still interesting to notice that there's no possible way to
write in Ruby a string like this:

""hello""


Regards,
Alvaro.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 2007, at 10:26 AM, Alvaro Perez wrote:

> I'm trying to export a string to a excel file using fastercsv and I
> wanted it double quote to make it safer.


FasterCSV handles all the quoting for you. That's why you use it.
So you should just be doing something like:

fcsv << %w[array of fields for row here]

James Edward Gray II


 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      08-16-2007
Alvaro Perez wrote:
> Hi all,
>
> I'm trying to export a string to a excel file using fastercsv and I
> wanted it double quote to make it safer. So i tried to use the escape
> character \" to insert double quotes but it doesn't work:
>
> - string produces string with no quotes in the file
>
> - "\"" << string << "\"" produces """string"""
>
> - "\"" + string + "\"" produces """string"""
>
> I have no idea on how to solve this.

You don't need to quote your output - FasterCSV quotes it iff necessary.
Try sticking a '"' in the middle of your string and see what it does.

--
Alex

 
Reply With Quote
 
thomas.macklin@gmail.com
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 11:26 am, Alvaro Perez <(E-Mail Removed)> wrote:
> Hi all,
>
> I'm trying to export a string to a excel file using fastercsv and I
> wanted it double quote to make it safer. So i tried to use the escape
> character \" to insert double quotes but it doesn't work:
>
> - string produces string with no quotes in the file
>
> - "\"" << string << "\"" produces """string"""
>
> - "\"" + string + "\"" produces """string"""
>
> I have no idea on how to solve this.
>
> Thanks for the help,
> Alvaro.
> --
> Posted viahttp://www.ruby-forum.com/.


try this:
irb
> str = "foo"
> str2 = %Q{#{str}}
> p str2


I think that's what you're looking for.

 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      08-16-2007
On 8/16/07, Alvaro Perez <(E-Mail Removed)> wrote:
> Hi all,
>
> I think there is not problem at all as it seems that the FasterCSV class
> is the one who's erasing the double quotes while converting the strings
> to cvs.
>
> Anyway, it's still interesting to notice that there's no possible way to
> write in Ruby a string like this:
>
> ""hello""


Sure there is.

'"hello"'
'""hello""'
"\"hello\""
"\"\"hello\"\""
%{"hello"}
%{""hello""}
<<-EOS
"hello"
""hello""
EOS

(Okay, that last one actually makes a multi-line string, but the point
remains the same.)

-austin
--
Austin Ziegler * http://www.velocityreviews.com/forums/(E-Mail Removed) * http://www.halostatue.ca/
* (E-Mail Removed) * http://www.halostatue.ca/feed/
* (E-Mail Removed)

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 2007, at 10:37 AM, Alvaro Perez wrote:

> Anyway, it's still interesting to notice that there's no possible
> way to
> write in Ruby a string like this:
>
> ""hello""


Sure there is:

%Q{"hello"}

James Edward Gray II

 
Reply With Quote
 
Harold Hausman
Guest
Posts: n/a
 
      08-16-2007
On 8/16/07, Alvaro Perez <(E-Mail Removed)> wrote:
> Anyway, it's still interesting to notice that there's no possible way to
> write in Ruby a string like this:
>
> ""hello""
>


irb(main):001:0> puts "\"\"hello\"\""
""hello""
=> nil

?,
-Harold

 
Reply With Quote
 
Alvaro Perez
Guest
Posts: n/a
 
      08-16-2007
It´s a bit strange this.

On my irb:

irb(main):053:0> p "\"hello\""
"\"hello\""

irb(main):049:0> h = '""hello""'
=> "\"\"hello\"\""

irb(main):044:0> string = "hello"
=> "hello"
irb(main):046:0> %Q{#{string}}
=> "hello"
irb(main):047:0> p string
"hello"

irb(main):048:0> puts "\"\"hello\"\""
""hello""


I´m not pretty sure, but the last one it´s the only that seems to
produce exactly what i was looking for...

Because "\"\"hello\"\"" != ""hello"" right? or it is altough irb shows
it in different ways?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      08-16-2007
On 8/16/07, Alvaro Perez <(E-Mail Removed)> wrote:
> It=B4s a bit strange this.
>
> On my irb:
>
> irb(main):053:0> p "\"hello\""
> "\"hello\""


puts "\"hello\"" # =3D> "hello"
puts "\"\"hello\"\"" # =3D> ""hello""

p uses #inspect, which usually escapes certain values. IRB uses #inspect, t=
oo.

-austin
--=20
Austin Ziegler * (E-Mail Removed) * http://www.halostatue.ca/
* (E-Mail Removed) * http://www.halostatue.ca/feed/
* (E-Mail Removed)

 
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
problem with single-quote and double-quote when using subprocess. Evan Python 3 11-04-2008 12:24 PM
Escape character in java How can use double quote in string Sanjeev Java 3 05-03-2008 06:59 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
Javascript double quote escape character? Alden Streeter ASP General 1 11-26-2003 09:45 PM



Advertisments