Removing carriage return/line field from a string

  • Thread starter Thread starter John Dalberg
  • Start date Start date
J

John Dalberg

Hi

What's the regex to remove the carriage return/line field from a string?
These can occur multiple times in the string as in xxx\r\n\r\n.
 
Use
myString = myString.Replace(System.Environment.NewLine, string.Empty);

System.Environment.NewLine is more portable than "\r\n", and Replace is
*far* faster than a regular expression.
 
John said:
Hi

What's the regex to remove the carriage return/line field from a string?
These can occur multiple times in the string as in xxx\r\n\r\n.

Warning: untested code.

Simple (but possibly a bit expensive):

string RemoveAndNewlineLineFeed(string s) {
char[] lf = {'\r', '\n'};
return string.Join("", string.Split(s, lf));
}

Note that (according to string.Split docs):
RemoveNewlineAndLineFeed("a\rb\nc\r\nd\n\re\r\r\n\r\n").Equals("abcde").
 
Back
Top