469,935 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

Removing carriage return/line field from a string


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.

--

John Dalberg
Nov 17 '05 #1
2 38183
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 Dalberg" <jo*****@hotmail.com> wrote in message
news:da***************************@40tude.net...

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.

--

John Dalberg

Nov 17 '05 #2
John Dalberg wrote:
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").

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Smythe32 | last post: by
3 posts views Thread by Gillian Steele | last post: by
11 posts views Thread by evenlater | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.