String Alignment Problem

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

    String Alignment Problem

    Friends,

    My Problem is related to writing a text file from a database
    using .Net. in a particular string format.
    I have a dataset with a table, let suppose in this table there are
    three fields.
    - Code
    - Description
    - Amount

    At the time of writing a text file I use in a for loop -

    String.Format(" {0,10}{1, 40} {2,-10}", DS.Table(0).Row (i)
    ("Code").ToStri ng(), DS.Table(0).Row (i)("Descriptio n").ToString (),
    DS.Table(0).Row (i)("Amount").T oString())


    But the problem is related to the alignment. Data comes like in
    following format:

    01Code 01Desc 2000
    02Code 02Desc 3000
    03CodeABC 03Desc 4000
    04xyz 04Desc 5000


    I want Data comes in following proper alignment format.

    01Code 01Desc 2000
    02Code 02Desc 3000
    03CodeABC 03Desc 4000
    04xyz 04Desc 5000


    So anybody knows the solutions of this problem, please and please
    reply imediately, its urgent.



    Rahul

  • Matthias Tacke

    #2
    Re: String Alignment Problem

    Rahul wrote:
    Friends,
    >
    My Problem is related to writing a text file from a database
    using .Net. in a particular string format.
    I have a dataset with a table, let suppose in this table there are
    three fields.
    - Code
    - Description
    - Amount
    >
    At the time of writing a text file I use in a for loop -
    >
    String.Format(" {0,10}{1, 40} {2,-10}", DS.Table(0).Row (i)
    ("Code").ToStri ng(), DS.Table(0).Row (i)("Descriptio n").ToString (),
    DS.Table(0).Row (i)("Amount").T oString())
    >
    String.Format(" {0,10}{1, 40} {2,-10}",
    does not cut to 10,40 and 10 places - if the content is longer it will
    puzzle your output.

    For testing I would insert a delimiter:
    String.Format(" {0,10}|{1, 40}|{2,-10}",
    But the problem is related to the alignment. Data comes like in
    following format:
    >
    01Code 01Desc 2000
    02Code 02Desc 3000
    03CodeABC 03Desc 4000
    04xyz 04Desc 5000
    >
    >
    I want Data comes in following proper alignment format.
    >
    01Code 01Desc 2000
    123456789012345 67
    Strange, why do you use {0,10} if it should be {0,17} ?
    02Code 02Desc 3000
    03CodeABC 03Desc 4000
    04xyz 04Desc 5000
    >
    >
    So anybody knows the solutions of this problem, please and please
    reply imediately, its urgent.
    >
    Use the format with proper number of places or use

    String.Format(" {0,10}{1, 40} {2,-10}", _
    Strings.Left(DS .Table(0).Row(i )("Code").ToStr ing(),10), _
    Strings.Left(DS .Table(0).Row(i )("Description" ).ToString(),40 ), _
    DS.Table(0).Row (i)("Amount").T oString())

    requires Imports Microsoft.Visua lBasic

    --
    Greetings
    Matthias

    Comment

    • Hal Rosser

      #3
      Re: String Alignment Problem


      "Rahul" <verma.career@g mail.comwrote in message
      news:1180933690 .339219.322990@ j4g2000prf.goog legroups.com...
      Friends,
      >
      My Problem is related to writing a text file from a database
      using .Net. in a particular string format.
      I have a dataset with a table, let suppose in this table there are
      three fields.
      - Code
      - Description
      - Amount
      >
      At the time of writing a text file I use in a for loop -
      >
      String.Format(" {0,10}{1, 40} {2,-10}", DS.Table(0).Row (i)
      ("Code").ToStri ng(), DS.Table(0).Row (i)("Descriptio n").ToString (),
      DS.Table(0).Row (i)("Amount").T oString())
      >
      >
      But the problem is related to the alignment. Data comes like in
      following format:
      >
      01Code 01Desc 2000
      02Code 02Desc 3000
      03CodeABC 03Desc 4000
      04xyz 04Desc 5000
      >
      >
      I want Data comes in following proper alignment format.
      >
      01Code 01Desc 2000
      02Code 02Desc 3000
      03CodeABC 03Desc 4000
      04xyz 04Desc 5000
      >
      >
      So anybody knows the solutions of this problem, please and please
      reply imediately, its urgent.
      >
      out of the many ways to do this, you may want to look at the padLeft and
      padRight methods of the String object.
      These methods can be used to left-align within a column - or right-align in
      a column. It adds (pads) spaces to the left or to the right of the content -
      you decide how many.
      I think these methods were made for programmers to generate reports.


      Comment

      • PlatinumBay

        #4
        Re: String Alignment Problem

        Rahul,

        One thing you could try is to pad each field using the String object's
        PadLeft and PadRight methods, such as:

        String.Format(" {0,10}{1, 40} {2,-10}",
        DS.Table(0).Row (i)("Code").ToS tring().PadRigh t(15, " "c),
        DS.Table(0).Row (i)("Descriptio n").ToString(). PadRight(15, " "c),
        DS.Table(0).Row (i)("Amount").T oString().PadRi ght(15, " "c))

        The downside to this is that you have to know ahead of time what the maximum
        length is for each field.


        Hope this helps,

        Steve


        "Rahul" <verma.career@g mail.comwrote in message
        news:1180933690 .339219.322990@ j4g2000prf.goog legroups.com...
        Friends,
        >
        My Problem is related to writing a text file from a database
        using .Net. in a particular string format.
        I have a dataset with a table, let suppose in this table there are
        three fields.
        - Code
        - Description
        - Amount
        >
        At the time of writing a text file I use in a for loop -
        >
        String.Format(" {0,10}{1, 40} {2,-10}", DS.Table(0).Row (i)
        ("Code").ToStri ng(), DS.Table(0).Row (i)("Descriptio n").ToString (),
        DS.Table(0).Row (i)("Amount").T oString())
        >
        >
        But the problem is related to the alignment. Data comes like in
        following format:
        >
        01Code 01Desc 2000
        02Code 02Desc 3000
        03CodeABC 03Desc 4000
        04xyz 04Desc 5000
        >
        >
        I want Data comes in following proper alignment format.
        >
        01Code 01Desc 2000
        02Code 02Desc 3000
        03CodeABC 03Desc 4000
        04xyz 04Desc 5000
        >
        >
        So anybody knows the solutions of this problem, please and please
        reply imediately, its urgent.
        >
        >
        >
        Rahul
        >

        Comment

        • Matthias Tacke

          #5
          Re: String Alignment Problem

          PlatinumBay wrote:
          ....
          String.Format(" {0,10}{1, 40} {2,-10}",
          DS.Table(0).Row (i)("Code").ToS tring().PadRigh t(15, " "c),
          DS.Table(0).Row (i)("Descriptio n").ToString(). PadRight(15, " "c),
          DS.Table(0).Row (i)("Amount").T oString().PadRi ght(15, " "c))
          >
          The downside to this is that you have to know ahead of time what the maximum
          length is for each field.
          >
          Hi Steve,
          Public Function PadRight(ByVal totalWidth As Integer) As String
          Member of System.String

          uses the spcae by default.

          The c in _" "c")_ in your above code seems to be a typo?


          --
          Greetings
          Matthias

          Comment

          • Andrew Morton

            #6
            Re: String Alignment Problem

            Matthias Tacke wrote:
            PlatinumBay wrote:
            ...
            >DS.Table(0).Ro w(i)("Code").To String().PadRig ht(15, " "c),
            The c in _" "c")_ in your above code seems to be a typo?
            It tells the computer that it's a single character rather than a string of
            one character.

            Andrew


            Comment

            Working...