Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Inserts HTML Line Breaks Before All Newlines In A String

DZone's Guide to

Inserts HTML Line Breaks Before All Newlines In A String

·
Free Resource

class String
  def nl2br!
    self.gsub("\n\r","
").gsub("\r", "").gsub("\n", "
") end end
Example: text 'line_1 line_2'.nl2br! Result in HTML: line_1
line_2
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}