Friday, January 11, 2013

Replace Line Breaks in a String C#

 Q: How can I replace Line Breaks within a string in C#?
 A:
public static class StringExtensions
{
   public static string RemoveLineBreaks( this string lines )
   {
      return lines.Replace( "\r", "").Replace( "\n", "" );
   }

   public static string ReplaceLineBreaks( this string lines, string replacement )
   {
      return lines.Replace( "\r\n", replacement )
                  .Replace( "\r", replacement )
                  .Replace( "\n", replacement );
   }
}

No comments:

Post a Comment