Learn how to a build a cloud-first strategyRegister Now


How to align string...

Posted on 2011-10-20
Medium Priority
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

Expert Comment

ID: 37002700
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

x77 earned 1000 total points
ID: 37002825
Use String.Format:

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

Expert Comment

by:Mike Tomlinson
ID: 37002908
LVL 83

Accepted Solution

CodeCruiser earned 1000 total points
ID: 37003332

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

864 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