473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2118
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...
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,
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...
6
by: Steve Richter | last post by:
I am using HtmlGenericControl to render html tags like <divand <span>. the problem is the <brtag does not render correctly when I use this method. ( it renders as <br></br>, which is seen by the...
7
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.