Home All Groups Group Topic Archive Search About

Formatting text w/VB.NET

Author
6 Sep 2006 4:29 PM
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

Author
6 Sep 2006 4:45 PM
Chris
Mark Brown wrote:
> 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(...)
Author
6 Sep 2006 5:39 PM
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.

Show quoteHide quote
"Chris" <no@spam.com> wrote in message news:44FEFB0E.1020505@spam.com...
> Mark Brown wrote:
>> 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(...)
Author
6 Sep 2006 7:51 PM
Chris Dunaway
Mark Brown wrote:
Show quoteHide quote
> 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.
>
> "Chris" <no@spam.com> wrote in message news:44FEFB0E.1020505@spam.com...
> > Mark Brown wrote:
> >> 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(...)

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
Author
6 Sep 2006 7:56 PM
Chris Dunaway
Chris Dunaway wrote:
Show quoteHide quote
> Mark Brown wrote:
> > 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.
> >
> > "Chris" <no@spam.com> wrote in message news:44FEFB0E.1020505@spam.com...
> > > Mark Brown wrote:
> > >> 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(...)
>
> 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.
Author
6 Sep 2006 9:21 PM
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

Show quoteHide quote
"Chris Dunaway" <dunaw***@gmail.com> wrote in message
news:1157572284.553191.59450@m79g2000cwm.googlegroups.com...
> Mark Brown wrote:
>> 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.
>>
>> "Chris" <no@spam.com> wrote in message news:44FEFB0E.1020505@spam.com...
>> > Mark Brown wrote:
>> >> 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(...)
>
> 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
>