Formating output text

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

    Formating output text

    Hi!

    I need to write some strings in text file, it consist 3 columns and it
    must looks like:

    ID Name Points
    =============== =======
    1 Mike 4,5
    2 Joseph 1,2
    3 Freddy 12,3

    The data is in listview control and i know how to get it from it
    but when when I put this records in file i can't make to format
    these record. I try to use .PadLeft and .PadRight but all i get is
    something like this:

    ID Name Points
    =============== =======
    1 Mike 4,5
    2 Joseph 1,2
    3 Freddy 12,3

    How to make that all columns are alligned?
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Formating output text

    On Apr 3, 3:53 pm, Joza <josip.pejako.. .@MAKNIOVOetfos .hrwrote:
    Hi!
    >
    I need to write some strings in text file, it consist 3 columns and it
    must looks like:
    >
    ID      Name    Points
    =============== =======
    1       Mike       4,5
    2       Joseph     1,2
    3       Freddy    12,3
    >
    The data is in listview control and i know how to get it from it
    but when when I put this records in file i can't make to format
    these record. I try to use .PadLeft and .PadRight but all i get is
    something like this:
    >
    ID      Name    Points
    =============== =======
    1       Mike       4,5
    2       Joseph      1,2
    3       Freddy      12,3
    >
    How to make that all columns are alligned?
    There are not alligned in the "corrected" example to begin with.
    In any case you will have to do it manually, I know of no provider
    that does this formatting (fixed length fields).
    It's very easy in fact, all you have to do is pritn the field value,
    and pad it with white spaces ( [size of the field] - [size of the
    actual value] ). Of course you have to check for overflow.

    Comment

    • Joza

      #3
      Re: Formating output text

      Ignacio Machin ( .NET/ C# MVP ) wrote:
      >
      There are not alligned in the "corrected" example to begin with.
      In any case you will have to do it manually, I know of no provider
      that does this formatting (fixed length fields).
      It's very easy in fact, all you have to do is pritn the field value,
      and pad it with white spaces ( [size of the field] - [size of the
      actual value] ). Of course you have to check for overflow.
      I try like this:

      string var="";
      string txt1 = "ABCD";
      string txt2 = "EFGHIJK";
      string num1 = "2.4";
      string num2 = "6.5";
      int myFieldSize = 35;

      Console.WriteLi ne(txt1 + var.PadRight(my FieldSize - num1.Length) + num1);
      Console.WriteLi ne(txt2 + var.PadRight(my FieldSize - num2.Length) + num2);

      And the result is something like:

      ABCD 2.4
      EFGHIJK 6.5

      instead of

      ABCD 2.4
      EFGHIJK 6.5

      I just don't get it... :(

      Comment

      • Joza

        #4
        Re: Formating output text

        Ignacio Machin ( .NET/ C# MVP ) wrote:
        >
        There are not alligned in the "corrected" example to begin with.
        In any case you will have to do it manually, I know of no provider
        that does this formatting (fixed length fields).
        It's very easy in fact, all you have to do is pritn the field value,
        and pad it with white spaces ( [size of the field] - [size of the
        actual value] ). Of course you have to check for overflow.
        I try like this:

        string var="";
        string txt1 = "ABCD";
        string txt2 = "EFGHIJK";
        string num1 = "2.4";
        string num2 = "6.5";
        int myFieldSize = 35;

        Console.WriteLi ne(txt1 + var.PadRight(my FieldSize - num1.Length) + num1);
        Console.WriteLi ne(txt2 + var.PadRight(my FieldSize - num2.Length) + num2);

        And the result is something like:

        ABCD 2.4
        EFGHIJK 6.5

        instead of

        ABCD 2.4
        EFGHIJK 6.5

        I just don't get it... :(


        Comment

        • Claes Bergefall

          #5
          Re: Formating output text


          "Joza" <josip.pejakovi c@MAKNIOVOetfos .hrwrote in message
          news:ft3kcq$fru $3@garrison.glo balnet.hr...
          Ignacio Machin ( .NET/ C# MVP ) wrote:
          >>
          >There are not alligned in the "corrected" example to begin with.
          >In any case you will have to do it manually, I know of no provider
          >that does this formatting (fixed length fields).
          >It's very easy in fact, all you have to do is pritn the field value,
          >and pad it with white spaces ( [size of the field] - [size of the
          >actual value] ). Of course you have to check for overflow.
          >
          I try like this:
          >
          string var="";
          string txt1 = "ABCD";
          string txt2 = "EFGHIJK";
          string num1 = "2.4";
          string num2 = "6.5";
          int myFieldSize = 35;
          >
          Console.WriteLi ne(txt1 + var.PadRight(my FieldSize - num1.Length) + num1);
          Console.WriteLi ne(txt2 + var.PadRight(my FieldSize - num2.Length) + num2);
          You need to use the first column when calculating how much padding to add,
          i.e.:
          Console.WriteLi ne(txt1 + var.PadRight(my FieldSize - txt1.Length) + num1);
          Console.WriteLi ne(txt2 + var.PadRight(my FieldSize - txt2.Length) + num2);

          Also note that this will only align correctly if you view it using a
          fixed-width font

          /claes



          Comment

          Working...