Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Removing Special Chars from a String

Reply
Thread Tools

Removing Special Chars from a String

 
 
Jeff Miller
Guest
Posts: n/a
 
      06-05-2008
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/.

 
Reply With Quote
 
 
 
 
Rodrigo Bermejo
Guest
Posts: n/a
 
      06-05-2008
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/.

 
Reply With Quote
 
 
 
 
Jeff Miller
Guest
Posts: n/a
 
      06-05-2008
thanks, that helped a lot! much appreciated!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-06-2008
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
 
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
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
RE: ascii character - removing chars from string update bruce Python 1 07-04-2006 06:54 AM
ascii character - removing chars from string bruce Python 6 07-04-2006 06:28 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM
Using special chars in std::string Thorsten Viel C++ 2 03-03-2004 05:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57