By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

Replace /n with a <br> help please

P: n/a
CK
Hi all,

I have a textarea control. I am putting it's value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn't work. Does anyone have
any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()

{

string s = tbxComments.Text;

Regex r = new Regex("/\n/g");

s = r.Replace(s, "<br/>");
return s;

}
Apr 17 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Have you tried replacing chr(10) and chr(13) instead of \n ?
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"CK" <c_**********@hotmail.com> wrote in message
news:jJ*******************@newssvr27.news.prodigy. net...
Hi all,

I have a textarea control. I am putting it's value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn't work. Does anyone
have any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()

{

string s = tbxComments.Text;

Regex r = new Regex("/\n/g");

s = r.Replace(s, "<br/>");
return s;

}

Apr 17 '06 #2

P: n/a
CK
I am using c# and not VB. Does c# use the chr() function? I do not think
so.
Any other ideas?

"Swanand Mokashi" <sw***********@swanandmokashi.com> wrote in message
news:eF*************@TK2MSFTNGP02.phx.gbl...
Have you tried replacing chr(10) and chr(13) instead of \n ?
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"CK" <c_**********@hotmail.com> wrote in message
news:jJ*******************@newssvr27.news.prodigy. net...
Hi all,

I have a textarea control. I am putting it's value in an html email.
The problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html
<br> tag. I tried the following function but it doesn't work. Does
anyone have any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()

{

string s = tbxComments.Text;

Regex r = new Regex("/\n/g");

s = r.Replace(s, "<br/>");
return s;

}


Apr 17 '06 #3

P: n/a
CK <c_**********@hotmail.com> wrote:
I have a textarea control. I am putting it's value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn't work. Does anyone have
any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()
{
string s = tbxComments.Text;
Regex r = new Regex("/\n/g");
s = r.Replace(s, "<br/>");
return s;
}


The first thing to do (IMO) is to get rid of the use of regular
expressions when they're unnecessary. I'm not a regex expert, so I
couldn't tell you without looking it up whether the above does what
you'd expect it to - but I *do* know that the following will do what
you'd expect it to:

string replaced = tbxComments.Text.Replace ("\n", "<br />");

Now, that only deals with line feeds, not carriage returns - but you
could always remove all carriage returns afterwards.

--
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
Apr 17 '06 #4

P: n/a
I like to create a text reader and a text writer for this task as you don't have to worry about what
the line terminator is. Just keep calling read line and write line.

Here's a method I wrote to handle this:

public static string ToHtmlNewLine(string text) {
if (text == null) return null;

int length;
StringReader reader;
StringWriter writer;
StringBuilder builder;
string line;

length = text.Length() * 1.2; //apply some padding to avoid array resizing, you probably want to
//tweak this value for the size of the strings you're using
reader = new StringReader(text);
builder = new StringBuilder(length);
writer = new StringWriter(builder) ;

line = reader.ReadLine();
if (line != null) {
/*this if then while loop avoids adding an extra blank line at the end of the conversion
* as opposed to just using:
* while (line != null) {
* writer.Write(line);
* writer.WriteLine("<br/>");
*/

writer.Write(line);
line = reader.ReadLine();

while (line != null) {
writer.WriteLine("<br/>");
writer.Write(line);
line = reader.ReadLine();
}
}

return writer.ToString();
}
CK wrote:
Hi all,

I have a textarea control. I am putting it's value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn't work. Does anyone have
any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()

{

string s = tbxComments.Text;

Regex r = new Regex("/\n/g");

s = r.Replace(s, "<br/>");
return s;

}

Apr 17 '06 #5

P: n/a
In that case have you tried replace \n as well as\r.
Also you can use Environment.NewLine instead of \n
"CK" <c_**********@hotmail.com> wrote in message
news:HZ******************@newssvr27.news.prodigy.n et...
I am using c# and not VB. Does c# use the chr() function? I do not think
so.
Any other ideas?

"Swanand Mokashi" <sw***********@swanandmokashi.com> wrote in message
news:eF*************@TK2MSFTNGP02.phx.gbl...
Have you tried replacing chr(10) and chr(13) instead of \n ?
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"CK" <c_**********@hotmail.com> wrote in message
news:jJ*******************@newssvr27.news.prodigy. net...
Hi all,

I have a textarea control. I am putting it's value in an html email.
The problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html
<br> tag. I tried the following function but it doesn't work. Does
anyone have any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()

{

string s = tbxComments.Text;

Regex r = new Regex("/\n/g");

s = r.Replace(s, "<br/>");
return s;

}



Apr 17 '06 #6

P: n/a
Chris Chilvers <ke****@dynafus.com> wrote:
I like to create a text reader and a text writer for this task as you
don't have to worry about what the line terminator is. Just keep
calling read line and write line.


That's a lot of work for:

string replaced = original.Replace("\r\n", "<br/>")
.Replace("\r", "<br/>")
.Replace("\n", "<br/>");

In some cases your method *may* be more efficient - but I'd have to see
evidence that it's actually significant in the actual real-world cases
before using a page of code instead of 3 simple lines.

--
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
Apr 17 '06 #7

P: n/a
Something which works for me for replacing carriage return linefeed combo:

string brChar;
string txtMessage;

brChar = Convert.ToString(Convert.ToChar(13)) +
Convert.ToString(Convert.ToChar(10));
txtMessage.Replace(m_brChar, "<br>")

--
brians
http://www.limbertech.com
"Jon Skeet [C# MVP]" wrote:
CK <c_**********@hotmail.com> wrote:
I have a textarea control. I am putting it's value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn't work. Does anyone have
any ideas how to accomplish this? Thanks in advance. ~ CK

string getComments()
{
string s = tbxComments.Text;
Regex r = new Regex("/\n/g");
s = r.Replace(s, "<br/>");
return s;
}


The first thing to do (IMO) is to get rid of the use of regular
expressions when they're unnecessary. I'm not a regex expert, so I
couldn't tell you without looking it up whether the above does what
you'd expect it to - but I *do* know that the following will do what
you'd expect it to:

string replaced = tbxComments.Text.Replace ("\n", "<br />");

Now, that only deals with line feeds, not carriage returns - but you
could always remove all carriage returns afterwards.

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

Apr 17 '06 #8

P: n/a
brians[MCSD] <br********@discussions.microsoft.com> wrote:
Something which works for me for replacing carriage return linefeed combo:

string brChar;
string txtMessage;

brChar = Convert.ToString(Convert.ToChar(13)) +
Convert.ToString(Convert.ToChar(10));
txtMessage.Replace(m_brChar, "<br>")


That's great - but you don't need to go to all that trouble to get
brChar. It's just "\r\n".

--
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
Apr 17 '06 #9

P: n/a
The Environment.NewLine will probably do the same thing too :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
brians[MCSD] <br********@discussions.microsoft.com> wrote:
Something which works for me for replacing carriage return linefeed
combo:

string brChar;
string txtMessage;

brChar = Convert.ToString(Convert.ToChar(13)) +
Convert.ToString(Convert.ToChar(10));
txtMessage.Replace(m_brChar, "<br>")


That's great - but you don't need to go to all that trouble to get
brChar. It's just "\r\n".

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

Apr 17 '06 #10

P: n/a
Swanand Mokashi <sw***********@swanandmokashi.com> wrote:
The Environment.NewLine will probably do the same thing too :)


Only on Windows. The point of Environment.NewLine (and the reason it's
not a constant) is that it's the *system-dependent* new line string. On
Linux (under Mono, say) it would be "\n".

--
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
Apr 18 '06 #11

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i en meddelelse
news:MP************************@msnews.microsoft.c om...
Chris Chilvers <ke****@dynafus.com> wrote:
I like to create a text reader and a text writer for this task as you
don't have to worry about what the line terminator is. Just keep
calling read line and write line.


That's a lot of work for:

string replaced = original.Replace("\r\n", "<br/>")
.Replace("\r", "<br/>")
.Replace("\n", "<br/>");


Hi

I am curious as to why you use '\r' and '\n'? How do you know that the
original string your function receives has these characters as
representations of carriage-returns or new-lines? Does the "textarea
control" mentioned in the original post always use these characters on all
operating systems? (Is it different between windows and linux and unix and
mac os and ...?)

Peter
Apr 18 '06 #12

P: n/a
Peter Kirk wrote:
That's a lot of work for:

string replaced = original.Replace("\r\n", "<br/>")
.Replace("\r", "<br/>")
.Replace("\n", "<br/>");


I am curious as to why you use '\r' and '\n'? How do you know that the
original string your function receives has these characters as
representations of carriage-returns or new-lines? Does the "textarea
control" mentioned in the original post always use these characters on all
operating systems? (Is it different between windows and linux and unix and
mac os and ...?)


Well, using the combinations above will cover Linux, Windows and Mac
OSX.
There is a possibility of someone using a textbox that doesn't use
those line terminators, but it seems pretty unlikely to me.

Of course, if you only want to cope with one type of line terminator,
you can use just one Replace call :)

Jon

Apr 18 '06 #13

P: n/a
Jon Skeet [C# MVP] wrote:
Chris Chilvers <ke****@dynafus.com> wrote:
I like to create a text reader and a text writer for this task as you
don't have to worry about what the line terminator is. Just keep
calling read line and write line.


That's a lot of work for:

string replaced = original.Replace("\r\n", "<br/>")
.Replace("\r", "<br/>")
.Replace("\n", "<br/>");

In some cases your method *may* be more efficient - but I'd have to see
evidence that it's actually significant in the actual real-world cases
before using a page of code instead of 3 simple lines.


In my particular case I first wrote the method to read straight out of a file then output the
results straight to the text writer in a web control's render method. Since I already had such a
method I just borrowed the original one and overloaded it.
Apr 18 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.