Skip to content

Instantly share code, notes, and snippets.

@sandrock
Created September 6, 2012 08:29
Show Gist options
  • Save sandrock/3653055 to your computer and use it in GitHub Desktop.
Save sandrock/3653055 to your computer and use it in GitHub Desktop.
Inserts HTML line breaks (<br />) before all newlines
public static class StringTransformer
{
/// <summary>
/// Inserts HTML line breaks (&lt;br /&gt;) before all newlines.
/// </summary>
/// <param name="text">text containing lines</param>
/// <returns></returns>
public static string AddHtmlLineBreaks(this string text)
{
if (text == null)
return null;
if (text.Contains("\r\n"))
return text.Replace("\r\n", "<br />\r\n");
else if (text.Contains("\n"))
return text.Replace("\n", "<br />\n");
else if (text.Contains("\r"))
return text.Replace("\r", "<br />\r");
else
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment