Re: how to check CRLF in C#

K

Kevin Spencer

It is altogether safer all around to check for Environment.Newline

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

Jerry III said:
You do know that Replace method does not update the string but creates a new
one (which you have to store and use instead of the original string), don't
you? And that your code posted below only replaces carriage returns, not
CRLF pairs?

Jerry

Simon Chung-Jen Chuang said:
After my testing, it cannot work. I write a method to do this.

public static string Display(string BodyText)
{
StringBuilder retVal = new StringBuilder();
if ((BodyText != null) && (BodyText != String.Empty))
{
for (int i = 0; i < BodyText.Length; i++)
{
switch (BodyText)
{
case '\r':
retVal.Append("<BR>");
break;
default:
retVal.Append(BodyText);
break;
}
}
}
return retVal.ToString();
}

 
K

Kevin Spencer

Jerry,

The original message didsn't specify any such thing. The author used "CRLF"
in the title, but obviously didn't know exactly what he was looking for,
other than a method to replace new line character/combination in a string.
Using Environment.Newline will return the correct new line character or
combination for whatever platform one is working on, and is the safest thing
to use. That is what it's for. My advice was correct.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

Jerry III said:
Kevin that depends, the original question was how to replace CRLF pairs, not
platform specific new line character(s).

Jerry

Kevin Spencer said:
It is altogether safer all around to check for Environment.Newline

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
The more I learn, the less I know.

Jerry III said:
You do know that Replace method does not update the string but creates
a
new
one (which you have to store and use instead of the original string), don't
you? And that your code posted below only replaces carriage returns, not
CRLF pairs?

Jerry

After my testing, it cannot work. I write a method to do this.

public static string Display(string BodyText)
{
StringBuilder retVal = new StringBuilder();
if ((BodyText != null) && (BodyText != String.Empty))
{
for (int i = 0; i < BodyText.Length; i++)
{
switch (BodyText)
{
case '\r':
retVal.Append("<BR>");
break;
default:
retVal.Append(BodyText);
break;
}
}
}
return retVal.ToString();
}


 
J

Jerry III

As long as the files are created on the same platform - you're fine. And
while that's most likely going to be the case there's a chance it won't - a
windows app may read files created by a Unix (or even mainframe, EBCDIC is a
whole new story) app. Or when you move your app from Windows to Linux but
the input files are still going to be created on Windows. So you should
replace whatever your case is, both examples were correct and as I said, it
depends on what exactly are you trying to accomplish.

Jerry

Kevin Spencer said:
Jerry,

The original message didsn't specify any such thing. The author used "CRLF"
in the title, but obviously didn't know exactly what he was looking for,
other than a method to replace new line character/combination in a string.
Using Environment.Newline will return the correct new line character or
combination for whatever platform one is working on, and is the safest thing
to use. That is what it's for. My advice was correct.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
The more I learn, the less I know.

Jerry III said:
Kevin that depends, the original question was how to replace CRLF pairs, not
platform specific new line character(s).

Jerry
creates
a
new
one (which you have to store and use instead of the original string),
don't
you? And that your code posted below only replaces carriage returns, not
CRLF pairs?

Jerry

After my testing, it cannot work. I write a method to do this.

public static string Display(string BodyText)
{
StringBuilder retVal = new StringBuilder();
if ((BodyText != null) && (BodyText != String.Empty))
{
for (int i = 0; i < BodyText.Length; i++)
{
switch (BodyText)
{
case '\r':
retVal.Append("<BR>");
break;
default:
retVal.Append(BodyText);
break;
}
}
}
return retVal.ToString();
}


 

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

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top