{ literal with AppendFormat

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

    { literal with AppendFormat

    Hello all,
    I would like to use AppendFormat to replace tokens in a string with
    actual string values. The problem I am running into however is that my
    string uses {} around a string that I do not want formatted. Is there
    a way to tell the AppendFormat method not to see certain {} as a token
    to be replaced?

    In the example here, I want to replace {0} with the string "Test" but
    leave the string {SQL Server} just like it is in the string.

    string MBConnectBase = @"{0} and {SQL Server}";
    try
    {
    System.Text.Str ingBuilder builder = new
    System.Text.Str ingBuilder();

    string format = MBConnectBase;
    builder.AppendF ormat(format, "Test");

    System.Diagnost ics.Debug.Write Line(builder.To String());
    }
    catch(System.Ex ception exc)
    {
    MessageBox.Show (exc.Message.To String());
    }

  • Carl Daniel [VC++ MVP]

    #2
    Re: { literal with AppendFormat

    "ramhog" <kalvin.tuel@ng c.comwrote in message
    news:1162935746 .078202.170640@ k70g2000cwa.goo glegroups.com.. .
    Hello all,
    I would like to use AppendFormat to replace tokens in a string with
    actual string values. The problem I am running into however is that my
    string uses {} around a string that I do not want formatted. Is there
    a way to tell the AppendFormat method not to see certain {} as a token
    to be replaced?
    >
    In the example here, I want to replace {0} with the string "Test" but
    leave the string {SQL Server} just like it is in the string.
    >
    string MBConnectBase = @"{0} and {SQL Server}";
    try
    {
    System.Text.Str ingBuilder builder = new
    System.Text.Str ingBuilder();
    >
    string format = MBConnectBase;
    builder.AppendF ormat(format, "Test");
    >
    System.Diagnost ics.Debug.Write Line(builder.To String());
    }
    catch(System.Ex ception exc)
    {
    MessageBox.Show (exc.Message.To String());
    }
    >
    Just double-up the { }

    string MBConnectBase = @"{0} and {{SQL Server}}";

    -cd



    Comment

    • ramhog

      #3
      Re: { literal with AppendFormat

      Thank you for your reply.
      Is there an escape sequence or any other options. Just making sure I
      have my bases covered.

      - k

      Carl Daniel [VC++ MVP] wrote:
      "ramhog" <kalvin.tuel@ng c.comwrote in message
      news:1162935746 .078202.170640@ k70g2000cwa.goo glegroups.com.. .
      Hello all,
      I would like to use AppendFormat to replace tokens in a string with
      actual string values. The problem I am running into however is that my
      string uses {} around a string that I do not want formatted. Is there
      a way to tell the AppendFormat method not to see certain {} as a token
      to be replaced?

      In the example here, I want to replace {0} with the string "Test" but
      leave the string {SQL Server} just like it is in the string.

      string MBConnectBase = @"{0} and {SQL Server}";
      try
      {
      System.Text.Str ingBuilder builder = new
      System.Text.Str ingBuilder();

      string format = MBConnectBase;
      builder.AppendF ormat(format, "Test");

      System.Diagnost ics.Debug.Write Line(builder.To String());
      }
      catch(System.Ex ception exc)
      {
      MessageBox.Show (exc.Message.To String());
      }
      >
      Just double-up the { }
      >
      string MBConnectBase = @"{0} and {{SQL Server}}";
      >
      -cd

      Comment

      • Carl Daniel [VC++ MVP]

        #4
        Re: { literal with AppendFormat

        "ramhog" <kalvin.tuel@ng c.comwrote in message
        news:1162936599 .395252.249060@ h54g2000cwb.goo glegroups.com.. .
        Thank you for your reply.
        Is there an escape sequence or any other options. Just making sure I
        have my bases covered.
        The doubled {{ is the only escape sequence - no other characters besides { }
        have specific meaning in a composite format string.

        -cd


        Comment

        Working...