Ruby remove empty lines from string

后端 未结 5 1979
囚心锁ツ
囚心锁ツ 2021-02-03 10:51

How do i remove empty lines from a string? I have tried some_string = some_string.gsub(/^$/, \"\");

and much more, but nothing works.

5条回答
  •  無奈伤痛
    2021-02-03 11:03

    Replace multiple newlines with a single one:

    fixedstr = str.gsub(/\n\n+/, "\n") 
    

    or

    str.gsub!(/\n\n+/, "\n") 
    

提交回复
热议问题