473,405 Members | 2,300 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,405 software developers and data experts.

replacing crlf with c#

I am using a c# utility to create a Java function on our web pages. I am
utilizing an object we created to retrieve a comment field from our SQL
database and have the system throw the desired alert when a particular option
is selected. The comment is stored in a Text field on our SQL server.
My problem is that, when I apply the value to the alert, the Java Script
throws and unterminated string constant when the page is run if and only if
the comment field contains CrLf in it's value.
I am using .replace(VBCrLf," \n ") from my ASP code behind to display create
an alert on loadup of the page if the current selected value has an alert
comment associated with it. However I can not seem to create an c#
counterpart in my utility
I have tried the following code
char cr = (char)13;
char lf = (char)10;
string cl = cr.tostring() + lf.tostring();

comment.replace(cl.tostring(), " \n ")
or
comment.replace(cr.tostring(),"").replace(lf.tostr ing()," \n ")
while both of these options process without error and create my javascript
function they do not seem to identify the CrLf characters and replace them
with my desired java escape values.

I know I can get around the issue by putting the code in our asp code behind
but then we won't be able to reutilize it as easily on other pages.
We are trying to standardize our utilities in c#.
I would prefer not to have to create separate SQL query statements to have
SQL handle the replacement.

What do I need to do to create a successful C# equivalent to VB's
comment.replace(vbCrLf, " \n ")

Any help would be greatly appreciated.

Eli Silverman

Oct 12 '06 #1
6 21286
What do I need to do to create a successful C# equivalent to VB's
comment.replace(vbCrLf, " \n ")
What about comment.replace("\r\n", " \n ")?

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 12 '06 #2
I have not actually tried it as it seems counterintuitive.
If I want to replace a Lf with the string "\n" i would say
comment.replace("\n","\n")
To me that seems like it would replace it with itself.
In all fairness however I should give it a try as it certainly won't perform
any worse than what I already have.

"Dustin Campbell" wrote:
What do I need to do to create a successful C# equivalent to VB's
comment.replace(vbCrLf, " \n ")

What about comment.replace("\r\n", " \n ")?

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 12 '06 #3
Eli Silverman wrote:
>
What do I need to do to create a successful C# equivalent to VB's
comment.replace(vbCrLf, " \n ")
You would need this:

comment = comment.Replace("\r\n", @" \n ");

Note that the @ symbol tells C# *not* to escape the \ character and to
insert it literally.

Oct 12 '06 #4
*Sound of hand smacking forehead*

Thanks Chris.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 12 '06 #5
Thanks Chris. That worked perfectly.

"Chris Dunaway" wrote:
Eli Silverman wrote:

What do I need to do to create a successful C# equivalent to VB's
comment.replace(vbCrLf, " \n ")

You would need this:

comment = comment.Replace("\r\n", @" \n ");

Note that the @ symbol tells C# *not* to escape the \ character and to
insert it literally.

Oct 12 '06 #6
Eli Silverman <El**********@discussions.microsoft.comwrote:
I have not actually tried it as it seems counterintuitive.
If I want to replace a Lf with the string "\n" i would say
comment.replace("\n","\n")
To me that seems like it would replace it with itself.
That's not what Dustin posted though. He posted to replace "\r\n" with
"\n", in other words, replacing carriage-return followed by line-feed
with just line-feed.

It's the way to go - but don't forget that it won't change the contents
of the original string, it'll return a new string with the replaced
data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: McKirahan | last post by:
How would I use a regular expression to remove all trailing Carriage Returns and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance. Also, are they any great references for learning...
5
by: McKirahan | last post by:
I'd like to use regular expressions to remove extraneous Carriage Return Line Feeds (CrLf) from a textarea before the form is submitted. I'd like to remove all trailing CrLf's and convert all...
2
by: Jerome | last post by:
Hi, I know this is an old question but I don't find the solution on the 'net ... The user enters his data into a multiline textarea field. The data is saved in a TEXT field in a database on a...
4
by: John | last post by:
Hi How can I replace characters like carriage return and double quotes from strings? Thanks Regards
1
by: Jack Wright | last post by:
Dear All, In a multiline field if I enter the following way: Mangesh Anant Kodilkar There is a carriage return after each word. Now when I press save,it loses the CRLF characters. This is not...
2
by: Michael Persaud | last post by:
Hi All, Im trying to have a few strings presented in a LABEL. However the CRLF is not working. eg str = str1 + crlf + str2 + crlf + str3 label.text= str Do i need to declare the crlf as a...
2
by: Andrew | last post by:
Hello. I am working with very large files in my .NET project (1 million plus records). Some of these files do not have crlf's, which makes it very hard to read the file without a layout or record...
14
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I...
6
by: Andrew | last post by:
I am using an XmlSerializer to save some settings and I have discovered that when a string is saved containing a cr+nl it is replaced with just a newline when loading back in. I am no expert with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.