By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,537 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,537 IT Pros & Developers. It's quick & easy.

specify length of a string variable

P: n/a
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

Mar 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shawrie wrote:
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
Perhaps PadLeft and PadRight would help?

Andrew
Mar 12 '07 #2

P: n/a
On Mar 12, 9:16 am, "shawrie" <neil.s...@sanderson.comwrote:
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
Mar 12 '07 #3

P: n/a
This syntax also works.

Dim test As New String(" "c, 14)

=============
Clay Burch
Syncfusion, Inc.

Mar 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.