473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 55390
How about this:

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

Regards,
Alex
"Howard Dean" <de******@white house.gov> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.r rzn.uni-hannover.de...
How about this:

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

Regards,
Alex
"Howard Dean" <de******@white house.gov> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.WriteLi ne( "Source:\n{ 0}", source );
Regex r = new Regex( pattern, RegexOptions.Si ngleline );
string newtext = r.Replace( source, "\x20" );
Regex fixup = new Regex( fixupPattern );
newtext = fixup.Replace( newtext, "\x20" );

Console.WriteLi ne( "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
9882
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 carriage returns in places where they have meaning. Attached are a sample input file, the sample output file, and a simplified version of my Java program. My real file, examines certain element's attributes and adds certain elements to the DOM...
3
2030
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 returns so I can have the address on a single line (possibly separated by commas) in a different database. Any suggestions gratefully received. Apologies if you replied to this earlier, I think my ISP must have dropped the newsgroup a week or two...
6
1733
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
5615
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 need to send this data file to the Unix machine once I remove the carriage retunrns from my XP machine. Thanks
8
2223
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 Browser. I want to then display this text within <p> tags when requested from the database. However, the test is formatted without the carriage returns.
7
11199
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 comment through a pop-up that contains a textarea. When the user hits OK on the pop-up, the text in the textarea is sent to a function on the main page. The function inserts the text into the div's text node. Please don't ask why I'm making this...
6
13544
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
4651
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 the email generated by getURL("mailto..") is fine in my email client. Carriage returns are kept etc and shows how I want it. However when I upload it to the server and run it on the web, the carriage returns are stripped out of the text and all...
3
2468
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 I navigate to the url in the browser and do a "View Source"? Or do I have to manually strip these out? The full code is below where I'm posting the necessary data to simulate a form post. HttpWebRequest req =...
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.