removing quotes in a string

G

Grace Xue

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

Thanks

Grace
 
P

Peña, Botp

RnJvbTogR3JhY2UgWHVlIFttYWlsdG86Z3JhY2VAd2hvc3RvbGVteW1vbmV5LmNvbV0gDQojIEhp
LCBkb2VzIGFueW9uZSBrbm93IG9mIGEgZ29vZCB3YXkgdG8gcmVtb3ZlIGxlYWRpbmcgYW5kIA0K
IyB0cmFpbGluZyBxdW90ZXMgKCIpIGluIGEgc3RyaW5nIHBsZWFzZT8NCg0KaXJiKG1haW4pOjAz
NzowPiBzPSUoInRoaXMgaXMgYSB0ZXN0IikNCj0+ICJcInRoaXMgaXMgYSB0ZXN0XCIiDQppcmIo
bWFpbik6MDM4OjA+IHMuZ3N1YiEoL14iKC4qPykiJC8sJ1wxJykNCj0+ICJ0aGlzIGlzIGEgdGVz
dCINCmlyYihtYWluKTowMzk6MD4gcw0KPT4gInRoaXMgaXMgYSB0ZXN0Ig0KaXJiKG1haW4pOjA0
MDowPg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
J

Joel VanderWerf

Grace said:
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.
 
G

Grace Xue

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
 
S

Skye Shaw!@#$

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)
 
P

Phil Meier

Grace said:
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
 
P

Peña, Botp

From: Daniel N [mailto:[email protected]]=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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top