Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > removing quotes in a string

Reply
Thread Tools

removing quotes in a string

 
 
Grace Xue
Guest
Posts: n/a
 
      08-16-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Peña, Botp
Guest
Posts: n/a
 
      08-16-2007
RnJvbTogR3JhY2UgWHVlIFttYWlsdG86Z3JhY2VAd2hvc3RvbG VteW1vbmV5LmNvbV0gDQojIEhp
LCBkb2VzIGFueW9uZSBrbm93IG9mIGEgZ29vZCB3YXkgdG8gcm Vtb3ZlIGxlYWRpbmcgYW5kIA0K
IyB0cmFpbGluZyBxdW90ZXMgKCIpIGluIGEgc3RyaW5nIHBsZW FzZT8NCg0KaXJiKG1haW4pOjAz
NzowPiBzPSUoInRoaXMgaXMgYSB0ZXN0IikNCj0+ICJcInRoaX MgaXMgYSB0ZXN0XCIiDQppcmIo
bWFpbik6MDM4OjA+IHMuZ3N1YiEoL14iKC4qPykiJC8sJ1wxJy kNCj0+ICJ0aGlzIGlzIGEgdGVz
dCINCmlyYihtYWluKTowMzk6MD4gcw0KPT4gInRoaXMgaXMgYS B0ZXN0Ig0KaXJiKG1haW4pOjA0
MDowPg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-16-2007
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

 
Reply With Quote
 
Grace Xue
Guest
Posts: n/a
 
      08-16-2007
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-forum.com/.

 
Reply With Quote
 
Skye Shaw!@#$
Guest
Posts: n/a
 
      08-16-2007
On Aug 15, 9:27 pm, Grace Xue <(E-Mail Removed)> 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)

 
Reply With Quote
 
Phil Meier
Guest
Posts: n/a
 
      08-16-2007
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
 
Reply With Quote
 
Pea, Botp
Guest
Posts: n/a
 
      08-16-2007
From: Daniel N [(E-Mail Removed)]=20
# 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'
=3D> true
irb(main):002:0> s=3D"\"\"\"asdf\"\""
=3D> "\"\"\"asdf\"\""
irb(main):003:0> s=3D"\"\"\"asdf\"\"".trim("\"")
=3D> "asdf"

kind regards -botp

 
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
regex to avoid escaped quotes or double quotes jOhn Ruby 1 01-29-2008 08:31 PM
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
Multiline quotes - escaping quotes - et al Lawrence Tierney Java 3 12-24-2003 05:12 PM



Advertisments