Removing Special Chars from a String

Discussion in 'Ruby' started by Jeff Miller, Jun 5, 2008.

  1. Jeff Miller

    Jeff Miller Guest

    Hello,
    I'm pulling some fields out of an Oracle database and creating a CSV.
    However, some fields have \n and \r in them, which messes up my CSV. Is
    there a special function or a certain way I should try to get rid of
    them? I tried using chomp, chomp!, strip, and strip! but I can't seem to
    get it formatted the right way. An example string would be:

    "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more text"

    Any suggestions?

    Thanks,
    - Jeff
    --
    Posted via http://www.ruby-forum.com/.
    Jeff Miller, Jun 5, 2008
    #1
    1. Advertising

  2. Jeff Miller wrote:
    > Hello,
    > I'm pulling some fields out of an Oracle database and creating a CSV.
    > However, some fields have \n and \r in them, which messes up my CSV. Is
    > there a special function or a certain way I should try to get rid of
    > them? I tried using chomp, chomp!, strip, and strip! but I can't seem to
    > get it formatted the right way. An example string would be:
    >
    > "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more text"
    >
    > Any suggestions?
    >
    > Thanks,
    > - Jeff


    If you want to delete them use this:

    "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more
    text".gsub(/\r|\n/,"")

    You can use: require 'csv' , it allows you to create/modify CSV with \r
    & \n without messing up the data.

    -r.
    --
    Posted via http://www.ruby-forum.com/.
    Rodrigo Bermejo, Jun 5, 2008
    #2
    1. Advertising

  3. Jeff Miller

    Jeff Miller Guest

    Jeff Miller, Jun 5, 2008
    #3
  4. On 05.06.2008 02:14, Rodrigo Bermejo wrote:
    > Jeff Miller wrote:
    >> Hello,
    >> I'm pulling some fields out of an Oracle database and creating a CSV.
    >> However, some fields have \n and \r in them, which messes up my CSV. Is
    >> there a special function or a certain way I should try to get rid of
    >> them? I tried using chomp, chomp!, strip, and strip! but I can't seem to
    >> get it formatted the right way. An example string would be:
    >>
    >> "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more text"
    >>
    >> Any suggestions?
    >>
    >> Thanks,
    >> - Jeff

    >
    > If you want to delete them use this:
    >
    > "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more
    > text".gsub(/\r|\n/,"")


    This will glue together words that were only separated by special chars.
    This might be better:

    irb(main):001:0> s = "\nhere is some text\r\n\r\nsome more
    text\n\r\n\reven more text"
    => "\nhere is some text\r\n\r\nsome more text\n\r\n\reven more text"

    irb(main):002:0> s.gsub(/\s+/,' ')
    => " here is some text some more text even more text"
    irb(main):003:0> s.gsub(/\s+/,' ').strip
    => "here is some text some more text even more text"

    irb(main):004:0> s.gsub(/[\r\n]+/,' ')
    => " here is some text some more text even more text"
    irb(main):005:0> s.gsub(/[\r\n]+/,' ').strip
    => "here is some text some more text even more text"

    > You can use: require 'csv' , it allows you to create/modify CSV with \r
    > & \n without messing up the data.


    That's probably even better.

    Jeff, note also that there are some nice formatting capabilities in
    SQL*Plus so you might as well create your output from there.

    See http://tahiti.oracle.com/ for docs.

    Kind regards

    robert
    Robert Klemme, Jun 6, 2008
    #4
    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. Thorsten Viel

    Using special chars in std::string

    Thorsten Viel, Mar 3, 2004, in forum: C++
    Replies:
    2
    Views:
    374
    Mike Wahler
    Mar 3, 2004
  2. bruce
    Replies:
    6
    Views:
    828
    Simon Forman
    Jul 4, 2006
  3. bruce
    Replies:
    1
    Views:
    611
    Marc 'BlackJack' Rintsch
    Jul 4, 2006
  4. Hongyu
    Replies:
    9
    Views:
    902
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    226
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page