[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

removing quotes in a string

Grace Xue

8/16/2007 4:27:00 AM

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

Thanks

Grace
--
Posted via http://www.ruby-....

6 Answers

Peña, Botp

8/16/2007 4:44:00 AM

0

From: Grace Xue [mailto:grace@whostolemymoney.com]
# 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

Joel VanderWerf

8/16/2007 4:55:00 AM

0

Grace Xue 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Grace Xue

8/16/2007 5:37:00 AM

0

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

skye.shaw

8/16/2007 6:18:00 AM

0

On Aug 15, 9:27 pm, Grace Xue <gr...@whostolemymoney.com> 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)

Phil Meier

8/16/2007 7:11:00 AM

0

Grace Xue schrieb:
> 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

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

Peña, Botp

8/16/2007 7:30:00 AM

0

From: Daniel N [mailto:has.sox@gmail.com]
# string.gsub( /\A"/m, "" ).gsub( /"\Z/m, "" )
# Bit simplistic maybe

simple but i usually do that :) 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