473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 55345
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. Laurence Leff | last post by:
I am writing a Java program to read in XML file, modify some elements slightly, and then write it out. That XML file is prepared in Docbook. It works fine, except that it is disturbing the...
3
by: Dave | last post by:
I need to strip out carriage returns from a memo field. Basically, one of the commercial databases we have uses a memo field for the address, and what I'd like to do is get rid of the carriage...
6
by: Andy Kent | last post by:
What's the easiest way I can set up an export to strip out carriage returns from memo datatypes? Thanks Andy
12
by: Nimmy | last post by:
Hi, I have a data file and I want to remove Carriage returns. Any one has any C code/program which does this? I am working on Windows XP machine.....I don't have access to UNIX machine. But I...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
7
by: mattrapoport | last post by:
I have a page with a div on it. The div displays a user comment. When the user logs into this page, their current comment is pulled from a db and displayed in the div. The user can edit the...
6
by: shajias | last post by:
Hi, I am having a xml code like this <name> I am having a carriage return here. Second line with carriage return. This is the last line. </name>
2
by: GregBeagle | last post by:
Windows XP I have a simple form from which I want to generate an email with the contents of the form. I use carriage returns to format the content for readability. When I test it on my computer...
3
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I'm trying to download a webpage by using the HttpWebRequest. It returns the html source, however, it contains "\r\n", "\t" etc throughout the text. Is there a way to return the same HTML as when...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.