By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,660 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,660 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.