views:

1597

answers:

4

Hi All,

Please help me to solve this.

This is my string

str = "aaa\n\n\nbbb\n\nccc\ddd\n" means four blank lines, all together eight lines

I want to return this in one line

Output should be like this (aaabbbcccddd) in single line

I used various trim functions to get the output but still i am failing to do it. if Anyone know the possible way, please help on this.

Thanks -Samitha

+7  A: 
str.gsub(/\n/,'')
Matchu
Wow Its working this is what I wanted ..amazing.., I struggled lot, you guys solved it within 1minute. Thank you very much. I’m new to ruby language as well as to this site. Hopefully I’ll be able to solve my ruby related questions from all of you. Thanks again
The best way to say thank you would be to choose this answer as your request solution. ;)
Simone Carletti
yes exactly :) im using this solution, thanks
There's a little check mark next to it - that's what you click when you're using an answer ^_^
Matchu
+1  A: 
> str = "aaa\n\n\nbbb\n\nccc\ddd\n" 
=> "aaa\n\n\nbbb\n\ncccddd\n"
> str.gsub("\n", "")
=> "aaabbbcccddd"
mcl
Thanks lot mcl its working..
+3  A: 

The Ruby (and slightly less Perl-ish) way:

new_str = str.delete "\n"

...or if you want to do it in-place:

str.delete! "\n"
Lars Haugseth
+1 for not using unnecessary regexes.
molf
A: 

What about :

str.chop
and
str.chop!
Riba