Hello everyone can anyone please help me?
I basically want to set the length of a string variable to help
spacing my simple report out.
i tried
dim test as string(14)
but it didnt like that
Shawrie
You can use String.Format to specify how much space a string should be
formatted in. This example uses a string builder to build some column
titles then loops to 10 printing values in each column. The "{0,xx}"
part of the String.Format statement tells vb to right-justify the
value for arg0 and pad it, if necessary, with spaces until it reaches
a length of xx. If the value of xx is negative then the string will
be left justified.
Dim sbColumns As New System.Text.StringBuilder
Dim sbData As New System.Text.StringBuilder
' Build the columns string
sbColumns.Append(String.Format("{0,14}", "ColumnA"))
sbColumns.Append(String.Format("{0,20}", "ColumnB"))
sbColumns.Append(String.Format("{0,9}", "ColumnC"))
sbColumns.Append(String.Format("{0,12}",
"ColumnD")).AppendLine()
Console.WriteLine(sbColumns.ToString)
For i As Integer = 1 To 10
sbData.Append(String.Format("{0,14}", "Value" +
i.ToString))
sbData.Append(String.Format("{0,20}", "Value" +
i.ToString))
sbData.Append(String.Format("{0,9}", "Value" +
i.ToString))
sbData.Append(String.Format("{0,12}", "Value" +
i.ToString)).AppendLine()
Next
Console.WriteLine(sbData.ToString)
Console.ReadLine()
The code would output the values below (font may screw up the
formatting but just imagine they're neatly aligned!)
ColumnA ColumnB ColumnC ColumnD
Value1 Value1 Value1 Value1
Value2 Value2 Value2 Value2
Value3 Value3 Value3 Value3
Value4 Value4 Value4 Value4
Value5 Value5 Value5 Value5
Value6 Value6 Value6 Value6
Value7 Value7 Value7 Value7
Value8 Value8 Value8 Value8
Value9 Value9 Value9 Value9
Value10 Value10 Value10 Value10