469,270 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Simple question : how to strip carriage returns from a string?

I have a string with several carriage returns in it. For example:

"This is
my test
string"

I wish to convert it to "This is my test string" (remove all carriage
returns. Can someone tell me how to do this using the string.Replace
function? Thanks!
Nov 13 '05 #1
3 54962
How about this:

s = s.Replace("\n", "");

Regards,
Alex
"Howard Dean" <de******@whitehouse.gov> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have a string with several carriage returns in it. For example:

"This is
my test
string"

I wish to convert it to "This is my test string" (remove all carriage
returns. Can someone tell me how to do this using the string.Replace
function? Thanks!

Nov 13 '05 #2
You really should check for/replace ASCII 10 and 13 (Carrier Return and Line
Feed)
In C#: \r and \n

i.e.

s = s.Replace("\n","").Replace("\r","");

OR if you want to replace several chars you can use the overload for .Trim
and build a unicode array of the chars to remove instead of piling up
..Replaces

"Alex Bendig" <a.******@ieee.org> wrote in message
news:be**********@newsserver.rrzn.uni-hannover.de...
How about this:

s = s.Replace("\n", "");

Regards,
Alex
"Howard Dean" <de******@whitehouse.gov> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have a string with several carriage returns in it. For example:

"This is
my test
string"

I wish to convert it to "This is my test string" (remove all carriage
returns. Can someone tell me how to do this using the string.Replace
function? Thanks!


Nov 13 '05 #3
string pattern = @"[\n\r]";
string fixupPattern = @"\x20{2,}?";
string source = "this\n\ris a\r\ntest of\n\rsomething";

Console.WriteLine( "Source:\n{0}", source );
Regex r = new Regex( pattern, RegexOptions.Singleline );
string newtext = r.Replace( source, "\x20" );
Regex fixup = new Regex( fixupPattern );
newtext = fixup.Replace( newtext, "\x20" );

Console.WriteLine( "New Text:\n{0}", newtext );

This could probably be condensed to one regex.
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dr. Laurence Leff | last post: by
3 posts views Thread by Dave | last post: by
12 posts views Thread by Nimmy | last post: by
8 posts views Thread by TheDude5B | last post: by
7 posts views Thread by mattrapoport | last post: by
3 posts views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.