Replace /n with a <br> help please

C

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;

}
 
S

Swanand Mokashi

C

CK

I am using c# and not VB. Does c# use the chr() function? I do not think
so.
Any other ideas?
 
J

Jon Skeet [C# MVP]

CK said:
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.
 
C

Chris Chilvers

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();
}
 
S

Swanand Mokashi

In that case have you tried replace \n as well as\r.
Also you can use Environment.NewLine instead of \n
 
J

Jon Skeet [C# MVP]

Chris Chilvers said:
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.
 
G

Guest

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>")
 
J

Jon Skeet [C# MVP]

brians said:
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".
 
J

Jon Skeet [C# MVP]

Swanand Mokashi said:
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".
 
P

Peter Kirk

Jon Skeet said:
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
 
J

Jon Skeet [C# MVP]

Peter said:
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
 
C

Chris Chilvers

Jon said:
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.
 

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
474,000
Messages
2,570,248
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top