Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty lines from a string in ruby

Tags:

string

regex

ruby

I've gone through other similar questions and they dont seem to explain my problem.

My output ,right now is like this, I would like to remove empty lines from the string in ruby,

#    

CIRRUS LADIES NIGHT with DJ ROHIT

4th of JULY Party ft. DJ JASMEET @ I-Bar

Submerge Deep @ Pebble | Brute Force (Tuhin Mehta) | DJ Arpan (Opening)

Champagne Showers - DJs Panic & Nyth @ Blue Waves

THURSDAY PAST AND PRESENT @ Hint

and I want my output to be like this,

CIRRUS LADIES NIGHT with DJ ROHIT
4th of JULY Party ft. DJ JASMEET @ I-Bar
Submerge Deep @ Pebble | Brute Force (Tuhin Mehta) | DJ Arpan (Opening)
Champagne Showers - DJs Panic & Nyth @ Blue Waves
THURSDAY PAST AND PRESENT @ Hint

I've tried gsub /^$\n/,'' , gsub(/\n/,'') , squeeze("\n") and delete! "\n" to no avail.

Also,I forgot to mention that my string starts with a blank line, the # denotes a blank line before the first line,if that would change anything.

My String.inspect as requested,the content of the string has changed,though the issue is still the same.

string.inspect :

"\n\n\t\t\t\t\t\t\t\t\t"
"Tricky Tuesdays with DJ John @ Blend"
"\n\n\t\t\t\t\t\t\t\t\t"
"Bladder Buster Challenge with DJ Sean @ Star Rock"
"\n\n\t\t\t\t\t\t\t\t\t"
"Classic Rock Tuesday @ 10D - Chennai"
"\n\n\t\t\t\t\t\t\t\t\t"
"Vodka Night with DJ John @ Blend"
"\n\n\t\t\t\t\t\t\t\t\t"
"\"BOLLYWOOD WEDNESDAYS\" with DJ D Nash @ Candy Club"
"\n\n\t\t\t\t\t\t\t\t\t"
"RE - LAUNCH WEDNESDAY LADIES NIGHT @ ZODIAC"
"\n\n\t\t\t\t\t\t\t\t\t"
"Ladies Night @ 10 D - Chennai"
"\n\n\t\t\t\t\t\t\t\t\t"
"Wednesday Mayhem @ Dublin"
"\n\n\t\t\t\t\t\t\t\t\t"
like image 534
arvindravi Avatar asked Sep 25 '26 07:09

arvindravi


1 Answers

Here's my solution:

text.gsub(/\n+|\r+/, "\n").squeeze("\n").strip
like image 166
Matheus Moreira Avatar answered Sep 27 '26 19:09

Matheus Moreira