Removing quotes in a string

Hi, does anyone know of a good way to remove leading the trailing quotes
(") in a string please?

Thanks

Grace

ircamsimac:~ cbarrie$ irb
irb(main):001:0> string = ‘“This is a string”’
=> “"This is a string"”
irb(main):002:0> string.gsub!(‘"’, ‘’)
=> “This is a string”
irb(main):003:0>

On Aug 16, 2:29 pm, Grace X. [email protected]

cammo wrote:

ircamsimac:~ cbarrie$ irb
irb(main):001:0> string = ‘“This is a string”’
=> ““This is a string””
irb(main):002:0> string.gsub!(’"’, ‘’)
=> “This is a string”
irb(main):003:0>

I’m a newbie and not sure if I understand how the ’ and " and \ works.

Supposed my string is str = “hello world” does it mean I should do

str.gsub!(’"’,’’)

Would this remove all the other " in the middle of the string too?

thanks

Hi, Cam. Thanks for that. Though I’m trying to read a line out of a csv
file which has leading and trailing quotes, therefore I can not put each
supposed “column” inside the right place in the array. For example.

The row should be
10/08/07,hello world
So I put first column into date filed, and second column into
description field.

But sometimes it comes in as
“10/08/07,hello world”
Therefore, the usual parse[] doesnt work.

Any suggestions?

Cheers

Nono, those quote marks are simple there to show you that it is an
object of the string type.

When that is printed to the screen they won’t be there.
Try this, in your controller create an intance variable like so

def index
@string = “This is a string”
end

and then in your index.rhtml view that corresponds to your controller

<%= @string %>

note how the string is written as “This is a string” but as: This is a
string

So unless you defined your variable as

@string = ’ “This is a string” ’ # => NB the ’ "

You wont have to gsub the quotes off.
Just as array types are displayed in the terminal with [] on them
strings are with “”.

Hope this helps,
Cam

On Aug 16, 7:44 am, Grace X. [email protected]
wrote:

“10/08/07,hello world”
Therefore, the usual parse[] doesnt work.

Any suggestions?
Something like
CSV.parse( File.read(“myfile.txt”).gsub(/^“([^”]*)"$/, “\1”) )
might work. It should catch only rows beginning and ending with
quotes, not containing quotes. If your quoted lines also contain
escaped quotes like ", things get trickier.

If all quotes should be removed, doing just gsub(‘"’, ‘’) as
described in previous replies is easier.

Also worth taking a closer look at what creates the “sometimes”.
eg. are the files coming from different sources, or is the sometimes
because you are using different code to read the file.

It is not always obvious what is coming from the content of the file
and what is occurring from how you read it. Also, you may get
differences depending on the file system or application creating the
file.

If you open a file in a spreadsheet application to make some changes
and then re-save it. This can introduce " around some fields, which
may never actually have normally occurred.

Reading the file into a single string can sometimes be helpful

File.open(“myfile.csv”, “r”) { |f|
@s = f.read
}
you can then view the content directly on the console or use debug in
the template

Using a file viewer can also be helpful. This one is for windows:

You can just run wnbrowse.exe directly from the zip file if like me
you are reluctant to install things without trying them first.
This can be a safer way to edit the file too.

Some thoughts that you may find useful
Tonypm

Well that’s more to do with now parsing the CSV correctly, it’s now
creating 2 strings for you. You should be returning
“10/08/07”, “hello world”

The Rails Recipes book has a great Recipe for that.

On Aug 16, 3:44 pm, Grace X. [email protected]