format string

J

James

vb.net 2003

i used console.writeline to output to screen.

eg
console.writeline ("test1 : " & vbtab & v_test1)
console.writeline ("test2 : " & vbtab & v_test2)
etc etc

result becomes
test1: <tab> result1
test2: <tab> <tab> results2

Although i used a single vbtab , the results is "mis-aligned" because it
depends on v_test1 and v_test2 strings of characters.

What is the best way to put into console.writeline such that i can
pre-defined all vbtabs for best alighment ?

I prefer results to be like this in tabular format :

test1 : result1
test2: result2
etc etc
 
S

Stephany Young

At face value it would appear that the value stored in v_test2 contains a
leading tab character.

If this is the case then you could remove the tab by trimming the value
before outputting it, thus:

Console.Writeline("test2: " & vbtab & v_test2.Trim)

A tab character, rather than forcing the subsequent output to a specific
column, is interpreted by the display agent (output window, Notepad, etc.)
as a number of spaces.

If the labels are of varying length then you would get something like:

test1: result1
test2long: results2

which might also be undesirable.

If you know, in advance, the maximum length of the labels then you can add
extra formatting features such as:

Console.Writeline("{0,-16}" & vbtab & "{1}", "test1:", v_test1.Trim)
Console.Writeline("{0,-16}" & vbtab & "{1}", "test2long:", v_test2.Trim)

and the output would appear as:

test1: result1
test2long: results2

Check out the String.Format method for more information on formatting
features.
 
J

Jay B. Harlow [MVP - Outlook]

James,
As Stephany suggests, use a format string, that contains the proper padding.
When using padding as Stephany shows, I would not use vbTab (as the padding
gives that to you). Normally I move the format itself to a constant,
something like:

Const formatLine As String = "{0,-16} {1}"

Console.Writeline(formatLine, "test1:", v_test1.Trim)
Console.Writeline(formatLine, "test2long:", v_test2.Trim)

For details on the format string see:

http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconcompositeformatting.asp


Alternatively you can use String.PadLeft & String.PadRight to pad the
"label" to the respective # of spaces.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| vb.net 2003
|
| i used console.writeline to output to screen.
|
| eg
| console.writeline ("test1 : " & vbtab & v_test1)
| console.writeline ("test2 : " & vbtab & v_test2)
| etc etc
|
| result becomes
| test1: <tab> result1
| test2: <tab> <tab> results2
|
| Although i used a single vbtab , the results is "mis-aligned" because it
| depends on v_test1 and v_test2 strings of characters.
|
| What is the best way to put into console.writeline such that i can
| pre-defined all vbtabs for best alighment ?
|
| I prefer results to be like this in tabular format :
|
| test1 : result1
| test2: result2
| etc etc
|
|
|
|
|
|
|
 
H

Hal Rosser

one way is to pad the strings with leading (or trailing) spaces, so that
they are the same length each time.
 
J

Jay B. Harlow [MVP - Outlook]

Hal,
Which is what the PadLeft & PadRight I mentioned do:
Alternatively you can use String.PadLeft & String.PadRight to pad the
"label" to the respective # of spaces.


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| one way is to pad the strings with leading (or trailing) spaces, so that
| they are the same length each time.
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top