473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ 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.Tex t;

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

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

}
Apr 17 '06 #1
13 18421
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_**********@h otmail.com> wrote in message
news:jJ******** ***********@new ssvr27.news.pro digy.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.Tex t;

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******** *****@TK2MSFTNG P02.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_**********@h otmail.com> wrote in message
news:jJ******** ***********@new ssvr27.news.pro digy.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.Tex t;

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

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

}


Apr 17 '06 #3
CK <c_**********@h otmail.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.Tex t;
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.Tex t.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.co m>
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(s tring 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(te xt);
builder = new StringBuilder(l ength);
writer = new StringWriter(bu ilder) ;

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(li ne);
* writer.WriteLin e("<br/>");
*/

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

while (line != null) {
writer.WriteLin e("<br/>");
writer.Write(li ne);
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.Tex t;

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.New Line instead of \n
"CK" <c_**********@h otmail.com> wrote in message
news:HZ******** **********@news svr27.news.prod igy.net...
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******** *****@TK2MSFTNG P02.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_**********@h otmail.com> wrote in message
news:jJ******** ***********@new ssvr27.news.pro digy.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.Tex t;

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.Replac e("\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.co m>
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.ToStrin g(Convert.ToCha r(13)) +
Convert.ToStrin g(Convert.ToCha r(10));
txtMessage.Repl ace(m_brChar, "<br>")

--
brians
http://www.limbertech.com
"Jon Skeet [C# MVP]" wrote:
CK <c_**********@h otmail.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.Tex t;
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.Tex t.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.co m>
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********@dis cussions.micros oft.com> wrote:
Something which works for me for replacing carriage return linefeed combo:

string brChar;
string txtMessage;

brChar = Convert.ToStrin g(Convert.ToCha r(13)) +
Convert.ToStrin g(Convert.ToCha r(10));
txtMessage.Repl ace(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.co m>
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.New Line will probably do the same thing too :)

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
brians[MCSD] <br********@dis cussions.micros oft.com> wrote:
Something which works for me for replacing carriage return linefeed
combo:

string brChar;
string txtMessage;

brChar = Convert.ToStrin g(Convert.ToCha r(13)) +
Convert.ToStrin g(Convert.ToCha r(10));
txtMessage.Repl ace(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.co m>
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

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

Similar topics

4
7862
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 "languages" files and these are describes for things on my website. Example text looks like this:
6
12155
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 you and see if there are any improvments that i should make ;-) It should be fast and if possible compatible with todays modern browser-standards. It should be activated by the onload-event. This is my code, free for all to use:
7
19157
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 the browser. I even replaced all the newlines with <BR/> tags but eventhough I see the tags in my source, I don't get a line break in the output document. Any help would be greatly appreciated.
7
2747
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 tags will remain making the page safe (I have to convert the linefeeds to <BR>s because the Server.EncodeHTML does not do that it seems). The problem is that users can use a special tag when editing the top to specify an area of the tip that will...
15
2726
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
2533
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 the charcontrols option and chr(13) with success.. was using the following code: Dim s As String = MyTextbox.Text
1
3171
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 the charcontrols option and chr(13) with success.. was using the following code: Dim s As String = MyTextbox.Text
13
2138
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 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()
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2775
muto222
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.