# Removing Special Chars from a String

**URL:** <https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569>\
**Category:** Ruby\
**Created:** [June 5, 2008, 2:07am UTC](https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569 "2008-06-05T02:07:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [June 5, 2008, 2:07am UTC](https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569/1 "2008-06-05T02:07:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [June 5, 2008, 2:15am UTC](https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569/2 "2008-06-05T02:15:36Z")

</div>

Jeff M. 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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [June 5, 2008, 3:16am UTC](https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569/3 "2008-06-05T03:16:58Z")

</div>

thanks, that helped a lot! much appreciated!

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [June 6, 2008, 8:30am UTC](https://www.ruby-forum.com/t/removing-special-chars-from-a-string/139569/4 "2008-06-06T08:30:37Z")

</div>

On 05.06.2008 02:14, Rodrigo B. wrote:

> > 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/](http://tahiti.oracle.com/) for docs.

Kind regards

robert
