How to align string...

Posted on 2011-10-20
Last Modified: 2012-05-12

I need to align string on the receipt.


Name           Quantity                    Price            Sum

xxxxxxxxx     1.00                       1.00            1.00
yyyyyyyyy     10.00                       10.00            100.00
zzzzzzzzz     1.00                       1.00            1.00

how to align yyyyyy so that end result look like:

Name           Quantity                    Price            Sum

xxxxxxxxx     1.00                       1.00            1.00
yyyyyyyyy   10.00                     10.00       100.00
zzzzzzzzz     1.00                       1.00            1.00

Currently i am playing with padleft and padright, but i just can get it.

Name string is always up to 20 characters,  others can vary but they will rarely be over 8 characters each, except maybe total which can get up to 10 characters.

Can i set fixed positions, let's say Name can be from 0 to 20, Quantity from 24-32..etc and inside those parameters make them align left or right so that numbers are nicely aligned?
Question by:Grogel
    LVL 6

    Expert Comment

    for column one
    For i = 0 to columwidth
        ' if the data to be aligned left
         if left align and i = 0 then
             string = data
             i = length of data + 1
         ' if the data to be aligned on right
         elseif right align and i = columnwidth - datalen
             string = string + data
             i = columnwidth
        ' for both case apped the
             string = string + " "
        end if
    next i
    do this for rest of the columns
    Now you have the first row ready in the string
    now loop it for each row of data

    column widht can be adjusted at any time with column witdh variable

    Hope this logic helps you
    LVL 15

    Assisted Solution

    Use String.Format:

    string.Format ("{0,-10} {1,8:n2} {2,8:n2}","xx",1.1,1.15)
    "xx             1,10     1,15"
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    LVL 83

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now