string.Format encoding?

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

    string.Format encoding?

    Suppose I have the following code:

    string myFormat = "Line1/nLine 2";
    string formattedString = string.Format(m yFormat);

    ....that would produce a 2-line output as expected.

    But if I load that very same format string from an xml file:

    ....load xmlNode WorkNode...
    string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
    string formattedString = string.Format(m yFormat);

    ....the result is a one-line "Line1/nLine 2" output.

    How can I get the string.Format method to handle control characters correctly?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: string.Format encoding?

    Scewbedew,

    That's the ting, it isn't the same format string. The same string in an
    XML file would have the character code 10 (newline), not "\n" in it. You
    need to insert the newline character into your XML, not use "\n".

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    "Scewbedew" <Scewbedew@disc ussions.microso ft.comwrote in message
    news:BFF0576C-301F-4408-82AB-77C56A61F137@mi crosoft.com...
    Suppose I have the following code:
    >
    string myFormat = "Line1/nLine 2";
    string formattedString = string.Format(m yFormat);
    >
    ...that would produce a 2-line output as expected.
    >
    But if I load that very same format string from an xml file:
    >
    ...load xmlNode WorkNode...
    string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
    string formattedString = string.Format(m yFormat);
    >
    ...the result is a one-line "Line1/nLine 2" output.
    >
    How can I get the string.Format method to handle control characters
    correctly?
    >
    >

    Comment

    • Scewbedew

      #3
      Re: string.Format encoding?

      Oh, I thought that string formatting was done by string.Format.. .silly me ;-)

      I can't change the xml file, so I guess I'd have to do some subsititutions
      of my own. Thanks for the info.

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      Scewbedew,
      >
      That's the ting, it isn't the same format string. The same string in an
      XML file would have the character code 10 (newline), not "\n" in it. You
      need to insert the newline character into your XML, not use "\n".
      >
      Hope this helps.
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      >
      "Scewbedew" <Scewbedew@disc ussions.microso ft.comwrote in message
      news:BFF0576C-301F-4408-82AB-77C56A61F137@mi crosoft.com...
      Suppose I have the following code:

      string myFormat = "Line1/nLine 2";
      string formattedString = string.Format(m yFormat);

      ...that would produce a 2-line output as expected.

      But if I load that very same format string from an xml file:

      ...load xmlNode WorkNode...
      string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
      string formattedString = string.Format(m yFormat);

      ...the result is a one-line "Line1/nLine 2" output.

      How can I get the string.Format method to handle control characters
      correctly?
      >
      >
      >

      Comment

      • Otis Mukinfus

        #4
        Re: string.Format encoding?

        On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
        <Scewbedew@disc ussions.microso ft.comwrote:
        >Suppose I have the following code:
        >
        >string myFormat = "Line1/nLine 2";
        >string formattedString = string.Format(m yFormat);
        >
        >...that would produce a 2-line output as expected.
        >
        >But if I load that very same format string from an xml file:
        >
        >...load xmlNode WorkNode...
        >string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
        >string formattedString = string.Format(m yFormat);
        >
        >...the result is a one-line "Line1/nLine 2" output.
        >
        >How can I get the string.Format method to handle control characters correctly?
        >
        Interesting. Did you use the debugger to look at the string you got from the
        xml file to verify that you actually got what you expected?
        Good luck with your project,

        Otis Mukinfus


        Comment

        • Bruce Wood

          #5
          Re: string.Format encoding?


          Scewbedew wrote:
          Suppose I have the following code:
          >
          string myFormat = "Line1/nLine 2";
          string formattedString = string.Format(m yFormat);
          >
          ...that would produce a 2-line output as expected.
          >
          But if I load that very same format string from an xml file:
          >
          ...load xmlNode WorkNode...
          string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
          string formattedString = string.Format(m yFormat);
          >
          ...the result is a one-line "Line1/nLine 2" output.
          >
          How can I get the string.Format method to handle control characters correctly?
          Probably a typo, but you posted "/n" instead of "\n". However, the more
          important point is that you misunderstand who is doing what.

          string.Format is not resolving \n into a newline character. The
          compiler is doing that. In other words, the variable strFormat does not
          contain:

          L-i-n-e-1-\-n-L-i-n-e-2

          In fact, it contains this:

          L-i-n-e-1-newline-L-i-n-e-2

          because the compiler has already resolved the \n notation into the
          corresponding control character. So, string.Format doesn't "handle" the
          \n notation at all. It just treats the newline like any other character
          and puts it in the output string.

          Now, when you read "Line1\nLin e2" from an XML file, what you're passing
          to string.Format is exactly that sequence of characters:

          L-i-n-e-1-\-n-L-i-n-e-2

          and so that's what it puts in the output string.

          So, the real question is how can you take a character string that may
          contain some character escapes, and translate them into the appropriate
          control characters. Perhaps someone else can answer that one.

          Comment

          • Scewbedew

            #6
            Re: string.Format encoding?

            Yes. In fact, I discovered exactly what Bruce Wood describes in another
            answer to my question: the xml originated string included all the characters
            unchanged, while the inline string included newline charactes instead of the
            "\n" characters.

            "Otis Mukinfus" wrote:
            On Thu, 28 Sep 2006 08:21:02 -0700, Scewbedew
            <Scewbedew@disc ussions.microso ft.comwrote:
            >
            Suppose I have the following code:

            string myFormat = "Line1/nLine 2";
            string formattedString = string.Format(m yFormat);

            ...that would produce a 2-line output as expected.

            But if I load that very same format string from an xml file:

            ...load xmlNode WorkNode...
            string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
            string formattedString = string.Format(m yFormat);

            ...the result is a one-line "Line1/nLine 2" output.

            How can I get the string.Format method to handle control characters correctly?
            Interesting. Did you use the debugger to look at the string you got from the
            xml file to verify that you actually got what you expected?
            Good luck with your project,
            >
            Otis Mukinfus


            >

            Comment

            • Scewbedew

              #7
              Re: string.Format encoding?

              Thanks for the clarification. You where correct about the typo, sorry about
              that. In my mind, it was sooo very obvious that string.Format should handle
              the formatting of the string, so I got really puzzled when it didn't happen.

              I have verified exactly the behaviour you describe while debugging my app,
              so it is now painfully obvious that my initial assumption was wrong (or...as
              a matter of fact I'm never wrong, it's only the world that's temporary out of
              sync...)

              If anyone has any good input to how to make the character translation I'm
              eager to take part of it.

              "Bruce Wood" wrote:
              >
              Scewbedew wrote:
              Suppose I have the following code:

              string myFormat = "Line1/nLine 2";
              string formattedString = string.Format(m yFormat);

              ...that would produce a 2-line output as expected.

              But if I load that very same format string from an xml file:

              ...load xmlNode WorkNode...
              string myFormat= WorkNode.Attrib utes["text"].InnerText.ToSt ring();
              string formattedString = string.Format(m yFormat);

              ...the result is a one-line "Line1/nLine 2" output.

              How can I get the string.Format method to handle control characters correctly?
              >
              Probably a typo, but you posted "/n" instead of "\n". However, the more
              important point is that you misunderstand who is doing what.
              >
              string.Format is not resolving \n into a newline character. The
              compiler is doing that. In other words, the variable strFormat does not
              contain:
              >
              L-i-n-e-1-\-n-L-i-n-e-2
              >
              In fact, it contains this:
              >
              L-i-n-e-1-newline-L-i-n-e-2
              >
              because the compiler has already resolved the \n notation into the
              corresponding control character. So, string.Format doesn't "handle" the
              \n notation at all. It just treats the newline like any other character
              and puts it in the output string.
              >
              Now, when you read "Line1\nLin e2" from an XML file, what you're passing
              to string.Format is exactly that sequence of characters:
              >
              L-i-n-e-1-\-n-L-i-n-e-2
              >
              and so that's what it puts in the output string.
              >
              So, the real question is how can you take a character string that may
              contain some character escapes, and translate them into the appropriate
              control characters. Perhaps someone else can answer that one.
              >
              >

              Comment

              Working...