Ruby remove empty lines from string

后端 未结 5 1930
囚心锁ツ
囚心锁ツ 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:17

    You could try to replace all occurrences of 2 or more line breaks with just one:

    my_string.gsub(/\n{2,}/, '\n')
    

提交回复
热议问题