Replace "\r\n" by "_"

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

    Replace "\r\n" by "_"

    Hi,

    I would like to replace "\r\n" by "_" within a specific string.

    I tried :
    strMyString.Rep lace('\r', '_');
    strMyString.Rep lace('\n', '_');
    or
    strMyString.Rep lace(System.Env ironment.NewLin e, '_');

    and it doesn't work, what is missing?

    Thank you!
    Marty
  • WRH

    #2
    Re: Replace "\r\n&quot ; by "_&quot ;

    Hello
    try...

    strMyString = strMyString.Rep lace("\r\n","_" );



    "Marty" <xmarty99@hotma il.com> wrote in message
    news:PzK7f.5513 8$Io.27031@clgr ps13...[color=blue]
    > Hi,
    >
    > I would like to replace "\r\n" by "_" within a specific string.
    >
    > I tried :
    > strMyString.Rep lace('\r', '_');
    > strMyString.Rep lace('\n', '_');
    > or
    > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
    > and it doesn't work, what is missing?
    >
    > Thank you!
    > Marty[/color]


    Comment

    • VJ

      #3
      Re: Replace &quot;\r\n&quot ; by &quot;_&quot ;

      Marty

      This works for me....

      string strText = "Text me " + Environment.New Line + "Text me " +
      Environment.New Line;
      strText = strText.Replace (Environment.Ne wLine, "_");

      or this works..

      string strText = "Text me " + Environment.New Line + "Text me " +
      Environment.New Line;

      strText = strText.Replace ("\r\n", "_");

      Vijay


      "Marty" <xmarty99@hotma il.com> wrote in message
      news:PzK7f.5513 8$Io.27031@clgr ps13...[color=blue]
      > Hi,
      >
      > I would like to replace "\r\n" by "_" within a specific string.
      >
      > I tried :
      > strMyString.Rep lace('\r', '_');
      > strMyString.Rep lace('\n', '_');
      > or
      > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
      > and it doesn't work, what is missing?
      >
      > Thank you!
      > Marty[/color]


      Comment

      • John Bowman

        #4
        Re: Replace &quot;\r\n&quot ; by &quot;_&quot ;

        Marty,

        You almost had it. The Replace() method returns the modified string. Try
        this...

        string strMyString = "This is\r\n some line\r wrapping\n text";
        strMyString = strMyString.Rep lace("\r\n", "_").Replace("\ r",
        "_").Replace("\ n", "_");

        John


        "Marty" <xmarty99@hotma il.com> wrote in message
        news:PzK7f.5513 8$Io.27031@clgr ps13...[color=blue]
        > Hi,
        >
        > I would like to replace "\r\n" by "_" within a specific string.
        >
        > I tried :
        > strMyString.Rep lace('\r', '_');
        > strMyString.Rep lace('\n', '_');
        > or
        > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
        > and it doesn't work, what is missing?
        >
        > Thank you!
        > Marty[/color]


        Comment

        • Jianwei Sun

          #5
          Re: Replace &quot;\r\n&quot ; by &quot;_&quot ;

          I think you have to assign it to a different string, this is a little
          bit differnet with the C++ string.

          Marty wrote:[color=blue]
          > Hi,
          >
          > I would like to replace "\r\n" by "_" within a specific string.
          >
          > I tried :
          > strMyString.Rep lace('\r', '_');
          > strMyString.Rep lace('\n', '_');
          > or
          > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
          >
          > and it doesn't work, what is missing?
          >
          > Thank you!
          > Marty[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Replace &quot;\r\n&quot ; by &quot;_&quot ;

            Marty wrote:[color=blue]
            > I would like to replace "\r\n" by "_" within a specific string.
            >
            > I tried :
            > strMyString.Rep lace('\r', '_');
            > strMyString.Rep lace('\n', '_');
            > or
            > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
            >
            > and it doesn't work, what is missing?[/color]

            Strings are immutable - String.Replace doesn't change the contents of
            the string you call it on; instead, it returns a new string.

            To achieve your stated aim, you should use:

            strMyString = strMyString.Rep lace("\r\n", "_");

            Note that your first attempt would replace bare carriage returns and
            bare line feeds with underscores, and would replace "\r\n" with "__"
            instead of "_".
            Your second attempt is closer, but will only replace "\r\n" on Windows
            - on other platforms, it will replace whatever the platform default
            newline string is with "_".

            Jon

            Comment

            • Marty

              #7
              Re: Replace &quot;\r\n&quot ; by &quot;_&quot ;

              Thanks guys for your help :)
              Marty

              Marty wrote:[color=blue]
              > Hi,
              >
              > I would like to replace "\r\n" by "_" within a specific string.
              >
              > I tried :
              > strMyString.Rep lace('\r', '_');
              > strMyString.Rep lace('\n', '_');
              > or
              > strMyString.Rep lace(System.Env ironment.NewLin e, '_');
              >
              > and it doesn't work, what is missing?
              >
              > Thank you!
              > Marty[/color]

              Comment

              Working...