Formatting a string to print out

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myo Thura Lwin
    New Member
    • Jan 2010
    • 7

    Formatting a string to print out

    Hello,

    I face one problem in printing out the strings.
    I show the example for easy reference.
    For the output results, I attached the text file because this page cannot show my example outputs that I want to mean.

    My Code

    string str = "";
    string[] fruit ={ "Orange", "Apple", "WaterMelon " };

    for (int i = 0; i < fruit.Length; i++)
    {
    str += string.Format(" {0} {1,5} {2,5} {3,5}" + Environment.New Line,
    fruit[i], "1", "2", "4");

    }

    Console.WriteLi ne(str);

    For output, see my attachment.


    So, please help me.

    And , for another condition,
    if the data to print are taken from the datarows in datatable, how can I write the code to get the output that I want.

    Thanks,
    Attached Files
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You need to format the fruit text as well... I went through MSDN stuff to find out how. Here's the link I looked at...



    Basically, you need to pad the right of your fruit text with spaces to make it a fixed width. You're already padding the left of your number text, so you know how to pad the fruit text. Just use a negative number to pad right instead of left (so weird!).

    For example...
    Code:
    string.Format("{0,-10}--", "test");

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I could also suggest using a tab ( \t ) instead of hard coding the spaces. This would make it more flexible when imported to spreadsheets and word processors.
      Excel for example will recognize the tab as a column break.

      str += string.Format(" {0}\t{1}\t{2}\t {3}\n",fruit[i], "1", "2", "4");

      Comment

      • Myo Thura Lwin
        New Member
        • Jan 2010
        • 7

        #4
        I really feel glad of using this web site and this program.
        Thanks a lot for both quick answers.

        Both are very useful and hit my target.

        But, in using tab(\t), I face some problem.
        Because, in my example,
        I think, the length of "Watermelon " takes one tab.
        So, the last line differs one tab from the upper two lines.

        So, how can I adjust ?

        Thanks

        Comment

        • Myo Thura Lwin
          New Member
          • Jan 2010
          • 7

          #5
          I really feel glad of using this web site and this program.
          Thanks a lot for both quick answers.

          Both are very useful and hit my target.

          But, in using tab(\t), I face some problem.
          Because, in my example,
          I think, the length of "Watermelon " takes one tab.
          So, the last line differs one tab from the upper two lines.

          So, how can I adjust ?

          Thanks

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            You can't, really, that's why you pad the string. I think Thlintoq was suggesting using a tab space to format for output in a spreadsheet, which you can do, but if you want to align columns in the text display you'd output it differently.

            If you just want the text display, pad the strings.

            Comment

            Working...