removing quotes in a string

Discussion in 'Ruby' started by Grace Xue, Aug 16, 2007.

  1. Grace Xue

    Grace Xue Guest

    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/.
     
    Grace Xue, Aug 16, 2007
    #1
    1. Advertising

  2. RnJvbTogR3JhY2UgWHVlIFttYWlsdG86Z3JhY2VAd2hvc3RvbGVteW1vbmV5LmNvbV0gDQojIEhp
    LCBkb2VzIGFueW9uZSBrbm93IG9mIGEgZ29vZCB3YXkgdG8gcmVtb3ZlIGxlYWRpbmcgYW5kIA0K
    IyB0cmFpbGluZyBxdW90ZXMgKCIpIGluIGEgc3RyaW5nIHBsZWFzZT8NCg0KaXJiKG1haW4pOjAz
    NzowPiBzPSUoInRoaXMgaXMgYSB0ZXN0IikNCj0+ICJcInRoaXMgaXMgYSB0ZXN0XCIiDQppcmIo
    bWFpbik6MDM4OjA+IHMuZ3N1YiEoL14iKC4qPykiJC8sJ1wxJykNCj0+ICJ0aGlzIGlzIGEgdGVz
    dCINCmlyYihtYWluKTowMzk6MD4gcw0KPT4gInRoaXMgaXMgYSB0ZXN0Ig0KaXJiKG1haW4pOjA0
    MDowPg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
     
    Peña, Botp, Aug 16, 2007
    #2
    1. Advertising

  3. 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
     
    Joel VanderWerf, Aug 16, 2007
    #3
  4. Grace Xue

    Grace Xue Guest

    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/.
     
    Grace Xue, Aug 16, 2007
    #4
  5. On Aug 15, 9:27 pm, Grace Xue <> 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)
     
    Skye Shaw!@#$, Aug 16, 2007
    #5
  6. Grace Xue

    Phil Meier Guest

    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
     
    Phil Meier, Aug 16, 2007
    #6
  7. Grace Xue

    Peña, Botp Guest

    From: Daniel N [mailto:]=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
     
    Peña, Botp, Aug 16, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,852
    Hermit Dave
    Sep 22, 2004
  2. Chris
    Replies:
    1
    Views:
    13,644
    Oisin
    Mar 24, 2006
  3. Lawrence Tierney

    Multiline quotes - escaping quotes - et al

    Lawrence Tierney, Dec 24, 2003, in forum: Java
    Replies:
    3
    Views:
    4,497
    Andrew Thompson
    Dec 24, 2003
  4. jOhn
    Replies:
    1
    Views:
    225
    Phlip
    Jan 29, 2008
  5. Richard Sandoval
    Replies:
    5
    Views:
    205
    7stud --
    Apr 26, 2011
Loading...

Share This Page