Formatting text w/VB.NET

M

Mark Brown

How do you format text in VB.NET? In VB6, I'd use something like:
format(foo,"!@@@@@@@@") to get a string to fill 8 places even if it was only
3 (for example). This simple convention apparently doesn't exist in VB.NET.
I've looked all around and I can't find a single example on how you would
code this in VB.NET. Anyone have an example they can share or point me to
some examples?

Thanks,
Mark
 
C

Chris

Mark said:
How do you format text in VB.NET? In VB6, I'd use something like:
format(foo,"!@@@@@@@@") to get a string to fill 8 places even if it was only
3 (for example). This simple convention apparently doesn't exist in VB.NET.
I've looked all around and I can't find a single example on how you would
code this in VB.NET. Anyone have an example they can share or point me to
some examples?

Thanks,
Mark

String.Format(...)
 
M

Mark Brown

Didn't work. I tried that with a string 7 chars long, tried to format it so
that it would always be at least 10 chars and it still only formatted to 7
chars.
 
C

Chris Dunaway

Mark said:
Didn't work. I tried that with a string 7 chars long, tried to format it so
that it would always be at least 10 chars and it still only formatted to 7
chars.

What did you try? Can you show the code? Remember that strings in
..Net are immutable and cannot be changed so methods like Format return
a new string. To format like you want you would do something like
this:

Dim s As String = "Hello"

s = String.Format("{0,-10}", s)

This line tells it to format the string, making it 10 characters if the
string is less than 10 characters. The minus sign tells it to left
justify the text. If you want it right justified, remove the minus
sign.

You can also do something like this:

Dim sName As String = "Mark"
Dim sResult As String = String.Format("Hello there {0,-10}", sName)

sResult would contain: "Hello there Mark "

Hope this helps
 
C

Chris Dunaway

Chris said:
What did you try? Can you show the code? Remember that strings in
.Net are immutable and cannot be changed so methods like Format return
a new string. To format like you want you would do something like
this:

Dim s As String = "Hello"

s = String.Format("{0,-10}", s)

This line tells it to format the string, making it 10 characters if the
string is less than 10 characters. The minus sign tells it to left
justify the text. If you want it right justified, remove the minus
sign.

You can also do something like this:

Dim sName As String = "Mark"
Dim sResult As String = String.Format("Hello there {0,-10}", sName)

sResult would contain: "Hello there Mark "

Hope this helps

I should have pointed out that you can use multiple argument to the
Format command like this:

Dim sFirstName As String = "Mark"
Dim sLastName As String = "Brown"

Dim sResult As String

sResult = String.Format("First Name: {0,-10} Last Name: {1,-10}",
sFirstName, sLastName)

Notice that the 0 and 1 in the format specifiers are indexes to the
arguments passed into the Format method. 0 corresponds to the
sFirstName, 1 corresponds to the last name. You can use them more then
once in a format too like this:

Dim sFirstName As String = "Mark"
Dim sLastName As String = "Brown"

Dim sResult As String

sResult = String.Format("First Name: {0,-10} Last Name: {1,-10} Full
Name: {0} {1}", sFirstName, sLastName)

Notice how I used the 0 and 1 indexes in the string format.
 
M

Mark Brown

Well, I don't know what I did wrong the first time, but I tried it again and
it worked just fine.

Thanks!

Mark
 

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