Posts tagged with replace

C# Replace New Lines

October 29th, 2009

If you know you’re working on the Windows platform and all data will come from Windows then to replace newlines using C#.NET we just use.

myString=myString.Replace(Environment.NewLine,"");

In all other cases it’s much safer to string together the Replace function to cover all possibilities.

myString=myString.Replace("\r", "").Replace("\n", "");
Google BookmarksEvernoteGoogle GmailHotmailWordPressLinkedInFacebookDeliciousShare

Remove DOS newlines from Python files

September 17th, 2008

I’ve been working on a few python files in WIndows and then moving them to Linux but whenever I try to run them I get an error about ‘directory not found’.

I soon discovered that the Windows carriage return messes up the files for Linux so we need to remove them all. If you open the offending file using vi you’ll see lots of ^M characters spread through the file. Fortunately these are pretty easy to remove using vi. Open the file in vi, press escape so we can enter a command, type this command press enter and save the file.

%s/^M/ /g

It’s very important that you create the ^M character by typing alt+v then press m while still holding down alt+v, do not paste straight from this page.

Google BookmarksEvernoteGoogle GmailHotmailWordPressLinkedInFacebookDeliciousShare

Switch to our mobile site