Alignment and string.Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tstephan@gmail.com

    #1

    Alignment and string.Format

    In the docs for String.Format there is an alignment component which
    allows you to pad the field to a certain number of spaces.

    e.g.
    FormatFName = String.Format(" First Name = |{0,10}|", myFName);

    Pads myFName, width of 10 minimum

    I am used to printf which allows a '*' to be used to specify a
    variable to be used at runtime to specify the width of the column.

    e.g.
    printf("%*s", iColWidth, myFName);
    Pads myFName, width of iColWidth minimum

    Am I missing something or did we lose that power in C#? I would hate
    to have to build my format string at runtime just to do this.

  • Tom Porterfield

    #2
    Re: Alignment and string.Format

    tstephan@gmail. com wrote:
    In the docs for String.Format there is an alignment component which
    allows you to pad the field to a certain number of spaces.
    >
    e.g.
    FormatFName = String.Format(" First Name = |{0,10}|", myFName);
    >
    Pads myFName, width of 10 minimum
    >
    I am used to printf which allows a '*' to be used to specify a
    variable to be used at runtime to specify the width of the column.
    >
    e.g.
    printf("%*s", iColWidth, myFName);
    Pads myFName, width of iColWidth minimum
    >
    Am I missing something or did we lose that power in C#? I would hate
    to have to build my format string at runtime just to do this.
    >
    As far as I know, that's the only way. Ex:

    string myFName = "tom";
    int iColWidth = 10;
    string FormatFName = string.Format(s tring.Format("F irst Name =
    |{{0,{0}}}|", iColWidth), myFName);

    --
    Tom Porterfield

    Comment

    • tstephan@gmail.com

      #3
      Re: Alignment and string.Format

      On Aug 9, 3:38 pm, Tom Porterfield <tppor...@mvps. orgwrote:
      tstep...@gmail. com wrote:
      In the docs for String.Format there is an alignment component which
      allows you to pad the field to a certain number of spaces.
      >
      e.g.
      FormatFName = String.Format(" First Name = |{0,10}|", myFName);
      >
      Pads myFName, width of 10 minimum
      >
      I am used to printf which allows a '*' to be used to specify a
      variable to be used at runtime to specify the width of the column.
      >
      e.g.
      printf("%*s", iColWidth, myFName);
      Pads myFName, width of iColWidth minimum
      >
      Am I missing something or did we lose that power in C#? I would hate
      to have to build my format string at runtime just to do this.
      >
      As far as I know, that's the only way. Ex:
      >
      string myFName = "tom";
      int iColWidth = 10;
      string FormatFName = string.Format(s tring.Format("F irst Name =
      |{{0,{0}}}|", iColWidth), myFName);
      >
      --
      Tom Porterfield
      Thanks, that is exactly what I ended up doing - ugly code though. I
      am surprised .NET lost functionality however - it would have been easy
      to add this.

      Comment

      Working...