removing newline and other special characters from a string

If you are a regular follower of my blog you will have no doubt worked out I am doing some development over the last week and have been wrestling with various problems.

Today I found one of my columns had special characters such as Newline, carrage return etc and I wanted to remove them.  In the end I had to make this function and it did the job just fine.  I’m sure I have come across this problem before and I thought I had a better solution but searching on google and I couldn’t find anything.


   public static string ReplaceNewlines(string blockOfText, string replaceWith)
            return blockOfText.Replace("\r\n", replaceWith).Replace("\n", replaceWith).Replace("\r", replaceWith).Replace("\t", replaceWith);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s