replacing crlf with c#

G

Guest

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.tostring()," \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
 
D

Dustin Campbell

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
 
G

Guest

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.
 
C

Chris Dunaway

Eli said:
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.
 
D

Dustin Campbell

*Sound of hand smacking forehead*

Thanks Chris.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
G

Guest

Thanks Chris. That worked perfectly.



Chris Dunaway said:
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.
 
J

Jon Skeet [C# MVP]

Eli Silverman said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top