Replace /n with a <br> help please

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CK

    Replace /n with a <br> help please

    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;

    }


  • Swanand Mokashi

    #2
    Re: Replace /n with a &lt;br&gt; help please

    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)


    DotNetGenerics. com -- anything and everything about Microsoft .NET
    technology ...

    Welcome to our website ! Well, our domain name introduces you to one half of us. The other (and perhaps better ;)) half is Rashmi Upasani. Rashmi dwells from


    Home of the Stock Quotes, Quote of the day and Horoscope web services


    "CK" <c_kettenbach@h otmail.com> wrote in message
    news:jJQ0g.1658 7$tN3.15814@new ssvr27.news.pro digy.net...[color=blue]
    > 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;
    >
    > }
    >
    >[/color]


    Comment

    • CK

      #3
      Re: Replace /n with a &lt;br&gt; help please

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

      "Swanand Mokashi" <swanandNOSPAM@ swanandmokashi. com> wrote in message
      news:eFXkcqkYGH A.428@TK2MSFTNG P02.phx.gbl...[color=blue]
      > 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_kettenbach@h otmail.com> wrote in message
      > news:jJQ0g.1658 7$tN3.15814@new ssvr27.news.pro digy.net...[color=green]
      >> 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;
      >>
      >> }
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Replace /n with a &lt;br&gt; help please

        CK <c_kettenbach@h otmail.com> wrote:[color=blue]
        > 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;
        > }[/color]

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

        Comment

        • Chris Chilvers

          #5
          Re: Replace /n with a &lt;br&gt; help please

          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:[color=blue]
          > 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;
          >
          > }
          >
          >[/color]

          Comment

          • Swanand Mokashi

            #6
            Re: Replace /n with a &lt;br&gt; help please

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


            "CK" <c_kettenbach@h otmail.com> wrote in message
            news:HZQ0g.1659 3$tN3.1029@news svr27.news.prod igy.net...[color=blue]
            >I am using c# and not VB. Does c# use the chr() function? I do not think
            >so.
            > Any other ideas?
            >
            > "Swanand Mokashi" <swanandNOSPAM@ swanandmokashi. com> wrote in message
            > news:eFXkcqkYGH A.428@TK2MSFTNG P02.phx.gbl...[color=green]
            >> 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_kettenbach@h otmail.com> wrote in message
            >> news:jJQ0g.1658 7$tN3.15814@new ssvr27.news.pro digy.net...[color=darkred]
            >>> 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;
            >>>
            >>> }
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Replace /n with a &lt;br&gt; help please

              Chris Chilvers <keeper@dynafus .com> wrote:[color=blue]
              > 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.[/color]

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

              Comment

              • brians[MCSD]

                #8
                Re: Replace /n with a &lt;br&gt; help please

                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



                "Jon Skeet [C# MVP]" wrote:
                [color=blue]
                > CK <c_kettenbach@h otmail.com> wrote:[color=green]
                > > 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;
                > > }[/color]
                >
                > 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 - <skeet@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
                >[/color]

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Replace /n with a &lt;br&gt; help please

                  brians[MCSD] <briansMCSD@dis cussions.micros oft.com> wrote:[color=blue]
                  > 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>")[/color]

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

                  --
                  Jon Skeet - <skeet@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

                  Comment

                  • Swanand Mokashi

                    #10
                    Re: Replace /n with a &lt;br&gt; help please

                    The Environment.New Line will probably do the same thing too :)

                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1eae11 9b98bfef4f98d0e 0@msnews.micros oft.com...[color=blue]
                    > brians[MCSD] <briansMCSD@dis cussions.micros oft.com> wrote:[color=green]
                    >> 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>")[/color]
                    >
                    > That's great - but you don't need to go to all that trouble to get
                    > brChar. It's just "\r\n".
                    >
                    > --
                    > Jon Skeet - <skeet@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[/color]


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Replace /n with a &lt;br&gt; help please

                      Swanand Mokashi <swanandNOSPAM@ swanandmokashi. com> wrote:[color=blue]
                      > The Environment.New Line will probably do the same thing too :)[/color]

                      Only on Windows. The point of Environment.New Line (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 - <skeet@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

                      Comment

                      • Peter Kirk

                        #12
                        Re: Replace /n with a &lt;br&gt; help please


                        "Jon Skeet [C# MVP]" <skeet@pobox.co m> skrev i en meddelelse
                        news:MPG.1eae04 f3d56bb76498d0d d@msnews.micros oft.com...[color=blue]
                        > Chris Chilvers <keeper@dynafus .com> wrote:[color=green]
                        >> 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.[/color]
                        >
                        > That's a lot of work for:
                        >
                        > string replaced = original.Replac e("\r\n", "<br/>")
                        > .Replace("\r", "<br/>")
                        > .Replace("\n", "<br/>");[/color]

                        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


                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Replace /n with a &lt;br&gt; help please

                          Peter Kirk wrote:[color=blue][color=green]
                          > > That's a lot of work for:
                          > >
                          > > string replaced = original.Replac e("\r\n", "<br/>")
                          > > .Replace("\r", "<br/>")
                          > > .Replace("\n", "<br/>");[/color]
                          >
                          > 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 ...?)[/color]

                          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

                          Comment

                          • Chris Chilvers

                            #14
                            Re: Replace /n with a &lt;br&gt; help please

                            Jon Skeet [C# MVP] wrote:[color=blue]
                            > Chris Chilvers <keeper@dynafus .com> wrote:[color=green]
                            >> 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.[/color]
                            >
                            > 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.
                            >[/color]

                            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.

                            Comment

                            Working...