Solved

is it possible to format a string while appending lines to stringbuilder?

Posted on 2008-10-22
6
185 Views
Last Modified: 2013-12-17
hello experts,

for my test necessities,
i feed a stringbuilder in order to display values of object to the console.

how can i have the following lines formated as :

 if(EcowastePosition_ != null)
                {
                    sb.Append("\nAddress:"          + obj.Address);
                    sb.Append("\nAddressNo:"        + obj.AddressNo);
                    sb.Append("\nCity:"             + obj.PostalNo);
}

....
....
Console.write(sb.tostring());

i 'd like to view :
Address                anAddress
AddressNo            aNo
City                       aCity

i'd like to have the second column, the values, in a vertical line alined.

thank you..
0
Comment
Question by:toshi_
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 22774854
   sb.Append("\nAddress:\t\t"          + obj.Address);
                    sb.Append("\nAddressNo:\t\t"        + obj.AddressNo);
                    sb.Append("\nCity:\t\t"             + obj.PostalNo);
0
 

Author Comment

by:toshi_
ID: 22774879
it wouldnt work that easy , thank you for the reply
but my second column is not alined

is there another possibitliy?



0
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 22775033
Hello toshi_,

sb.Append("\nAddress:".PadRight(20)).Append(obj.Address);
sb.Append("\nAddressNo:".PadRight(20)).Append(obj.AddressNo);
etc

Regards,

TimCottee
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:toshi_
ID: 22775983
thank you TimCottee,
but i still missing something.
as example look at the display here under.

Id Ecopoint:.................1006
Id Ecolog:...................1572871
Id Weight System:............1572871

but even with the second parameter filled with '.' my columns are not alined,

is it possible to have a result as follow,?

Id Ecopoint:........................1006
Id Ecolog:...........................1572871
Id Weight System:............1572871

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 22776079
toshi_,

Didn't realise it was for HTML output. The issue here is most likely that you have a proportional font so any padding in this way is not going to have the desired effect. One solution to that would be to create a table within your  or either multiple columns using  or

sb.Append("\nAddress:").Append(obj.Address).Append("")
sb.Append("\nAddressNo:").Append(obj.AddressNo).Append("")
sb.Append("\nCity:").Append(obj.City).Append("").Append("")

Or

sb.Append("")
sb.Append("Address:AddressNo:City")
sb.Append("").Append(obj.Address).Append("").Append(obj.AddressNo).Append("").Append(obj.City).Append("")
sb.Appemd("")

TimCottee
0
 

Author Comment

by:toshi_
ID: 22776255
thank you very much timCottee,

i notice now , while debugging that on a fixed width font
the display is exactly what i was lookiing for....

i just need to change my NUnit to use a fixed width font.

cheers.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

861 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