473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Replace /n with a <br> help please

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
7
by: noor.rahman | last post by:
I have an XML file that stores data from an HTML form. I use XSL to display the data in HTML format. The data may have newline characters. However, XSL is not displaying the newlines properly in...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
15
by: tshad | last post by:
How do I go about this? I used to know this, but can't find VB.net replace that does this. Something like string.replace("<br>",NL) Thanks,
2
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
13
by: CK | last post by:
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...
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:
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...
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.