Removing quotes in a string

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

Thanks

Grace

On 8/16/07, Grace X. [email protected] wrote:

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

Thanks

Grace

One way would be to use two gsubs

string.gsub( /\A"/m, “” ).gsub( /"\Z/m, “” )

Bit simplistic maybe

HTH
Daniel

From: Grace X. [mailto:[email protected]]

Hi, does anyone know of a good way to remove leading and

trailing quotes (") in a string please?

irb(main):037:0> s=%(“this is a test”)
=> “"this is a test"”
irb(main):038:0> s.gsub!(/^“(.*?)”$/,‘\1’)
=> “this is a test”
irb(main):039:0> s
=> “this is a test”
irb(main):040:0>

kind regards -botp

Grace X. wrote:

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

A nondestructive way (returns a new string):

s = ““foo bar\nbaz””
s[/\A"(.*)"\z/m,1] # => “foo bar\nbaz”

It’s elegant, but it may not be the fastest.

On Aug 15, 9:27 pm, Grace X. [email protected] wrote:

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

Good as in terse, good as in fast, good as in creative…?

[sshaw@localhost ~]$ cat bs.rb
require ‘benchmark’

TIMES=1_000_000

Benchmark.bm() do |x|
x.report { 1.upto(TIMES) { s=%(“this is a test”); s.gsub!(/
^“(.?)“$/,‘\1’) } }
x.report { 1.upto(TIMES) { s=%(“this is a test”); s[/\A”(.
)”\z/m,
1] } }
x.report { 1.upto(TIMES) { s=%(“this is a test”);
s[1,s.length-2] } }
end

Benchmark.bm() do |x|
x.report { 1.upto(TIMES) { s=%(“this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%… but still not that long\n yah know!”); s.gsub!(/
^“(.?)“$/,‘\1’) } }
x.report { 1.upto(TIMES) { s=%(“this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%… but still not that long\n yah know!”); s[/
\A”(.
)”\z/m,1] } }
x.report { 1.upto(TIMES) { s=%(“this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%… but still not that long\n yah know!”);
s[1,s.length-2] } }
end

[sshaw@localhost ~]$ ruby bs.rb
user system total real
6.070000 0.140000 6.210000 ( 6.230485)
2.410000 0.180000 2.590000 ( 2.579902)
1.640000 0.140000 1.780000 ( 1.784841)
user system total real
7.780000 0.140000 7.920000 ( 7.924870)
5.080000 0.180000 5.260000 ( 5.258682)
1.720000 0.140000 1.860000 ( 1.861885)

Grace X. schrieb:

for row in csv_rows
I have a feeling that I need to remove the quotes before the
CSV.parse_row line. But how?

Thanks

csv_rows.each do | row |
parsed[]
if row[0] == ?"
row = row[1…-2]
end
CSV.parse_row(row, 0, parsed)
date_string = parsed[0] => 16/08/07
description = parsed[1] => working hard
end

You have to check row before parsing it. If it has a " at position 0 (at
the beginning) just remove the first and the last character of the
string.

BR Phil

Thanks for all that everyone. I’m a bit lost after a whole afternoon of
trail and error. What I’m trying to do is read a csv. file that has
different rows and multiple coloumns. The way it’s currently done is
putting each row into an array, and retrieve each column using the
element number.

For example, a file with a line like this (CSV)
16/08/07,working hard

My codes are like this:
for row in csv_rows
begin
parsed[]
CSV.parse_row(row, 0, parsed)
date_string = parsed[0] => 16/08/07
description = parsed[1] => working hard
end

The problem I have is some file come in as
“16/08/07,working hard”

I have a feeling that I need to remove the quotes before the
CSV.parse_row line. But how?

Thanks

From: Daniel N [mailto:[email protected]]

string.gsub( /\A"/m, “” ).gsub( /"\Z/m, “” )

Bit simplistic maybe

simple but i usually do that :slight_smile: it’s easy to debug since we’ve broken
down the method (think division of labor :). in fact, my trim fxn does
ltrim.rtrim sequence,

C:\family\ruby\trim>irb
irb(main):001:0> require ‘trim’
=> true
irb(main):002:0> s=“"""asdf""”
=> “"""asdf""”
irb(main):003:0> s=“"""asdf""”.trim(“"”)
=> “asdf”

kind regards -botp