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

String Formatting in VB.NET

P: n/a
Have a quick simple question:

dim x as string

x = "1234567890"

-------------------------------------------------------
VB 6

dim y as string

y = format(x,"(@@@) @@@ @@@@")
y is now ---------------------> (123) 456 7890

(think that is the correct VB 6 syntax)
-------------------------------------------------------
VB NET

How can I do this using formatting in VB.NET since the "@" and other
characters are no longer used. I would prefer to avoid verbose methods
- using LEFT, MID, RIGHT....

-------------------------------------------------------
TIA
Scott M.

Jun 21 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
http://msdn2.microsoft.com/en-us/lib...ng.format.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Have a quick simple question:

dim x as string

x = "1234567890"

-------------------------------------------------------
VB 6

dim y as string

y = format(x,"(@@@) @@@ @@@@")
y is now ---------------------> (123) 456 7890

(think that is the correct VB 6 syntax)
-------------------------------------------------------
VB NET

How can I do this using formatting in VB.NET since the "@" and other
characters are no longer used. I would prefer to avoid verbose methods
- using LEFT, MID, RIGHT....

-------------------------------------------------------
TIA
Scott M.

Jun 21 '06 #2

P: n/a
Thanks for the post.
I must be misreading something, without resorting to VB6 namespace - or
- writing my own formatting code. I would like to format the data in a
"string" object in the following manner - example phone number:

dim x as string
x = "7034449999"
(703) 444 9999

or

x = "703PRSWXYY"
(703) PRS WXYY

This does not appear to work ?
string.format("{0:(###) ### ####}", x)
neither does this ? (microsoft removed "@")
string format("{0:(@@@) @@@ @@@@}", x)

I know that I must be missing something simple in this, but from
Microsoft's documentation I do not understand the technique to format a
simple string object - without actually writing a function to format my
string which I could do in less time than I am writing this.

TIA
I appologize for my denseness,
Scott M.
Kevin Spencer wrote:
http://msdn2.microsoft.com/en-us/lib...ng.format.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Have a quick simple question:

dim x as string

x = "1234567890"

-------------------------------------------------------
VB 6

dim y as string

y = format(x,"(@@@) @@@ @@@@")
y is now ---------------------> (123) 456 7890

(think that is the correct VB 6 syntax)
-------------------------------------------------------
VB NET

How can I do this using formatting in VB.NET since the "@" and other
characters are no longer used. I would prefer to avoid verbose methods
- using LEFT, MID, RIGHT....

-------------------------------------------------------
TIA
Scott M.


Jun 22 '06 #3

P: n/a
This might be helpful:

http://msdn2.microsoft.com/en-us/library/a292he7t.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Thanks for the post.
I must be misreading something, without resorting to VB6 namespace - or
- writing my own formatting code. I would like to format the data in a
"string" object in the following manner - example phone number:

dim x as string
x = "7034449999"
(703) 444 9999

or

x = "703PRSWXYY"
(703) PRS WXYY

This does not appear to work ?
string.format("{0:(###) ### ####}", x)
neither does this ? (microsoft removed "@")
string format("{0:(@@@) @@@ @@@@}", x)

I know that I must be missing something simple in this, but from
Microsoft's documentation I do not understand the technique to format a
simple string object - without actually writing a function to format my
string which I could do in less time than I am writing this.

TIA
I appologize for my denseness,
Scott M.
Kevin Spencer wrote:
http://msdn2.microsoft.com/en-us/lib...ng.format.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
> Have a quick simple question:
>
> dim x as string
>
> x = "1234567890"
>
> -------------------------------------------------------
> VB 6
>
> dim y as string
>
> y = format(x,"(@@@) @@@ @@@@")
> y is now ---------------------> (123) 456 7890
>
> (think that is the correct VB 6 syntax)
> -------------------------------------------------------
> VB NET
>
> How can I do this using formatting in VB.NET since the "@" and other
> characters are no longer used. I would prefer to avoid verbose methods
> - using LEFT, MID, RIGHT....
>
> -------------------------------------------------------
> TIA
> Scott M.
>

Jun 22 '06 #4

P: n/a
Thanks for the post, with the link, taking a little while to get at it.
Apparently "msdn2" server has been busy for the last three and a half
hours. Would like to see a solution without manually having to break
up the string (left,right,mid) - almost be inclined to
override/overload the string object and add the old style formatting to
it, if that could be done to the string object. It would be a simple
replacement routine with a few masking rules.

A perplexed
Scott
Kevin Spencer wrote:
This might be helpful:

http://msdn2.microsoft.com/en-us/library/a292he7t.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Thanks for the post.
I must be misreading something, without resorting to VB6 namespace - or
- writing my own formatting code. I would like to format the data in a
"string" object in the following manner - example phone number:

dim x as string
x = "7034449999"
(703) 444 9999

or

x = "703PRSWXYY"
(703) PRS WXYY

This does not appear to work ?
string.format("{0:(###) ### ####}", x)
neither does this ? (microsoft removed "@")
string format("{0:(@@@) @@@ @@@@}", x)

I know that I must be missing something simple in this, but from
Microsoft's documentation I do not understand the technique to format a
simple string object - without actually writing a function to format my
string which I could do in less time than I am writing this.

TIA
I appologize for my denseness,
Scott M.
Kevin Spencer wrote:
http://msdn2.microsoft.com/en-us/lib...ng.format.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
> Have a quick simple question:
>
> dim x as string
>
> x = "1234567890"
>
> -------------------------------------------------------
> VB 6
>
> dim y as string
>
> y = format(x,"(@@@) @@@ @@@@")
> y is now ---------------------> (123) 456 7890
>
> (think that is the correct VB 6 syntax)
> -------------------------------------------------------
> VB NET
>
> How can I do this using formatting in VB.NET since the "@" and other
> characters are no longer used. I would prefer to avoid verbose methods
> - using LEFT, MID, RIGHT....
>
> -------------------------------------------------------
> TIA
> Scott M.
>


Jun 22 '06 #5

P: n/a
I am not too pleased with this solution. First I could not inherit
from string in the hopes of adding my own formatting to the string
object. But this is what I can glean to "format" this strings data:

dim x as string
x = "20060622163321"
y = String.Format("{0}\{1}\{2} {3}:{4}:{5}", Left(x, 4), Mid(x, 5, 2),
Mid(x, 7, 2), Mid(x, 9, 2), Mid(x, 11, 2), Mid(x, 13, 2))

with the "y" result being:
2006\06\22 16:33:21

Of course this will pop if the string is less than 14 characters.

Thanks again,
Scott

L. Scott M. wrote:
Thanks for the post, with the link, taking a little while to get at it.
Apparently "msdn2" server has been busy for the last three and a half
hours. Would like to see a solution without manually having to break
up the string (left,right,mid) - almost be inclined to
override/overload the string object and add the old style formatting to
it, if that could be done to the string object. It would be a simple
replacement routine with a few masking rules.

A perplexed
Scott
Kevin Spencer wrote:
This might be helpful:

http://msdn2.microsoft.com/en-us/library/a292he7t.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Thanks for the post.
I must be misreading something, without resorting to VB6 namespace - or
- writing my own formatting code. I would like to format the data in a
"string" object in the following manner - example phone number:

dim x as string
x = "7034449999"
(703) 444 9999

or

x = "703PRSWXYY"
(703) PRS WXYY

This does not appear to work ?
string.format("{0:(###) ### ####}", x)
neither does this ? (microsoft removed "@")
string format("{0:(@@@) @@@ @@@@}", x)

I know that I must be missing something simple in this, but from
Microsoft's documentation I do not understand the technique to format a
simple string object - without actually writing a function to format my
string which I could do in less time than I am writing this.

TIA
I appologize for my denseness,
Scott M.
Kevin Spencer wrote:
> http://msdn2.microsoft.com/en-us/lib...ng.format.aspx
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Professional Chicken Salad Alchemist
>
> I recycle.
> I send everything back to the planet it came from.
>
> "L. Scott M." <sc*******@comcast.net> wrote in message
> news:11*********************@c74g2000cwc.googlegro ups.com...
> > Have a quick simple question:
> >
> > dim x as string
> >
> > x = "1234567890"
> >
> > -------------------------------------------------------
> > VB 6
> >
> > dim y as string
> >
> > y = format(x,"(@@@) @@@ @@@@")
> > y is now ---------------------> (123) 456 7890
> >
> > (think that is the correct VB 6 syntax)
> > -------------------------------------------------------
> > VB NET
> >
> > How can I do this using formatting in VB.NET since the "@" and other
> > characters are no longer used. I would prefer to avoid verbose methods
> > - using LEFT, MID, RIGHT....
> >
> > -------------------------------------------------------
> > TIA
> > Scott M.
> >


Jun 22 '06 #6

P: n/a
Hi Scott,

Since you want to ensure that the format is correct, another option would be
to use a Regular Expression. A Regular Expression matches patterns in a
string. So, for example, if you wanted to format a phone number as "(@@@)
@@@ @@@@" you could use something ike the following:

(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})

What this regular expression says is: Find 3 digits followed by 0 or more
non-digits, followed by 3 digits, followed by 0 or more non-digits, followed
by 4 digits. It puts the first 3 digits into Group 1, the second 3 into
Group 2, and the last 4 into Group 3. I tested it against the following:

1234567890
123 456-7890
(123) 456 - 7890
12 Baker's Street 19005
123 45th Ave.
12-23-456-789-10
123 (456) abcde 890

It found matches in the first 3 items, but none in any of the others,
because they did not have the right number of digits in the right sequence.
The last item *will* match if you add one more digit at the end.

You can then use the Regex Class to add your formatting, by formatting each
Group in each Match.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I am not too pleased with this solution. First I could not inherit
from string in the hopes of adding my own formatting to the string
object. But this is what I can glean to "format" this strings data:

dim x as string
x = "20060622163321"
y = String.Format("{0}\{1}\{2} {3}:{4}:{5}", Left(x, 4), Mid(x, 5, 2),
Mid(x, 7, 2), Mid(x, 9, 2), Mid(x, 11, 2), Mid(x, 13, 2))

with the "y" result being:
2006\06\22 16:33:21

Of course this will pop if the string is less than 14 characters.

Thanks again,
Scott

L. Scott M. wrote:
Thanks for the post, with the link, taking a little while to get at it.
Apparently "msdn2" server has been busy for the last three and a half
hours. Would like to see a solution without manually having to break
up the string (left,right,mid) - almost be inclined to
override/overload the string object and add the old style formatting to
it, if that could be done to the string object. It would be a simple
replacement routine with a few masking rules.

A perplexed
Scott
Kevin Spencer wrote:
> This might be helpful:
>
> http://msdn2.microsoft.com/en-us/library/a292he7t.aspx
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Professional Chicken Salad Alchemist
>
> I recycle.
> I send everything back to the planet it came from.
>
> "L. Scott M." <sc*******@comcast.net> wrote in message
> news:11**********************@i40g2000cwc.googlegr oups.com...
> > Thanks for the post.
> > I must be misreading something, without resorting to VB6 namespace -
> > or
> > - writing my own formatting code. I would like to format the data in
> > a
> > "string" object in the following manner - example phone number:
> >
> > dim x as string
> > x = "7034449999"
> > (703) 444 9999
> >
> > or
> >
> > x = "703PRSWXYY"
> > (703) PRS WXYY
> >
> > This does not appear to work ?
> > string.format("{0:(###) ### ####}", x)
> > neither does this ? (microsoft removed "@")
> > string format("{0:(@@@) @@@ @@@@}", x)
> >
> > I know that I must be missing something simple in this, but from
> > Microsoft's documentation I do not understand the technique to format
> > a
> > simple string object - without actually writing a function to format
> > my
> > string which I could do in less time than I am writing this.
> >
> > TIA
> > I appologize for my denseness,
> > Scott M.
> >
> >
> > Kevin Spencer wrote:
> >> http://msdn2.microsoft.com/en-us/lib...ng.format.aspx
> >>
> >> --
> >> HTH,
> >>
> >> Kevin Spencer
> >> Microsoft MVP
> >> Professional Chicken Salad Alchemist
> >>
> >> I recycle.
> >> I send everything back to the planet it came from.
> >>
> >> "L. Scott M." <sc*******@comcast.net> wrote in message
> >> news:11*********************@c74g2000cwc.googlegro ups.com...
> >> > Have a quick simple question:
> >> >
> >> > dim x as string
> >> >
> >> > x = "1234567890"
> >> >
> >> > -------------------------------------------------------
> >> > VB 6
> >> >
> >> > dim y as string
> >> >
> >> > y = format(x,"(@@@) @@@ @@@@")
> >> > y is now ---------------------> (123) 456 7890
> >> >
> >> > (think that is the correct VB 6 syntax)
> >> > -------------------------------------------------------
> >> > VB NET
> >> >
> >> > How can I do this using formatting in VB.NET since the "@" and
> >> > other
> >> > characters are no longer used. I would prefer to avoid verbose
> >> > methods
> >> > - using LEFT, MID, RIGHT....
> >> >
> >> > -------------------------------------------------------
> >> > TIA
> >> > Scott M.
> >> >
> >

Jun 22 '06 #7

P: n/a
Thanks Kevin,

Thought that I had seen something on string patterns when I started
..NETting from VB6 this last year. It would have been nice if they
(Microsoft) referenced "regular expressions" in their "format"
documentation (I could have overread it :( ). Guess I have some
more reading to do, kind of reminds me of what I remember of "awk". It
would be helpful if someone documented the formatting abilities of
"regular expressions" and "Regex". For example I see from your and
MS's examples the use of the "\d" escape but it is not referenced in
their "character escapes" documentation - ah - there it is in the
"character classes". Now to implement it, see how it plays .......

Kevin thanks for your assitance,
Scott
Kevin Spencer wrote:
Hi Scott,

Since you want to ensure that the format is correct, another option would be
to use a Regular Expression. A Regular Expression matches patterns in a
string. So, for example, if you wanted to format a phone number as "(@@@)
@@@ @@@@" you could use something ike the following:

(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})

What this regular expression says is: Find 3 digits followed by 0 or more
non-digits, followed by 3 digits, followed by 0 or more non-digits, followed
by 4 digits. It puts the first 3 digits into Group 1, the second 3 into
Group 2, and the last 4 into Group 3. I tested it against the following:

1234567890
123 456-7890
(123) 456 - 7890
12 Baker's Street 19005
123 45th Ave.
12-23-456-789-10
123 (456) abcde 890

It found matches in the first 3 items, but none in any of the others,
because they did not have the right number of digits in the right sequence.
The last item *will* match if you add one more digit at the end.

You can then use the Regex Class to add your formatting, by formatting each
Group in each Match.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.

"L. Scott M." <sc*******@comcast.net> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
I am not too pleased with this solution. First I could not inherit
from string in the hopes of adding my own formatting to the string
object. But this is what I can glean to "format" this strings data:

dim x as string
x = "20060622163321"
y = String.Format("{0}\{1}\{2} {3}:{4}:{5}", Left(x, 4), Mid(x, 5, 2),
Mid(x, 7, 2), Mid(x, 9, 2), Mid(x, 11, 2), Mid(x, 13, 2))

with the "y" result being:
2006\06\22 16:33:21

Of course this will pop if the string is less than 14 characters.

Thanks again,
Scott

L. Scott M. wrote:
Thanks for the post, with the link, taking a little while to get at it.
Apparently "msdn2" server has been busy for the last three and a half
hours. Would like to see a solution without manually having to break
up the string (left,right,mid) - almost be inclined to
override/overload the string object and add the old style formatting to
it, if that could be done to the string object. It would be a simple
replacement routine with a few masking rules.

A perplexed
Scott
Kevin Spencer wrote:
> This might be helpful:
>
> http://msdn2.microsoft.com/en-us/library/a292he7t.aspx
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Professional Chicken Salad Alchemist
>
> I recycle.
> I send everything back to the planet it came from.
>
> "L. Scott M." <sc*******@comcast.net> wrote in message
> news:11**********************@i40g2000cwc.googlegr oups.com...
> > Thanks for the post.
> > I must be misreading something, without resorting to VB6 namespace -
> > or
> > - writing my own formatting code. I would like to format the data in
> > a
> > "string" object in the following manner - example phone number:
> >
> > dim x as string
> > x = "7034449999"
> > (703) 444 9999
> >
> > or
> >
> > x = "703PRSWXYY"
> > (703) PRS WXYY
> >
> > This does not appear to work ?
> > string.format("{0:(###) ### ####}", x)
> > neither does this ? (microsoft removed "@")
> > string format("{0:(@@@) @@@ @@@@}", x)
> >
> > I know that I must be missing something simple in this, but from
> > Microsoft's documentation I do not understand the technique to format
> > a
> > simple string object - without actually writing a function to format
> > my
> > string which I could do in less time than I am writing this.
> >
> > TIA
> > I appologize for my denseness,
> > Scott M.
> >
> >
> > Kevin Spencer wrote:
> >> http://msdn2.microsoft.com/en-us/lib...ng.format.aspx
> >>
> >> --
> >> HTH,
> >>
> >> Kevin Spencer
> >> Microsoft MVP
> >> Professional Chicken Salad Alchemist
> >>
> >> I recycle.
> >> I send everything back to the planet it came from.
> >>
> >> "L. Scott M." <sc*******@comcast.net> wrote in message
> >> news:11*********************@c74g2000cwc.googlegro ups.com...
> >> > Have a quick simple question:
> >> >
> >> > dim x as string
> >> >
> >> > x = "1234567890"
> >> >
> >> > -------------------------------------------------------
> >> > VB 6
> >> >
> >> > dim y as string
> >> >
> >> > y = format(x,"(@@@) @@@ @@@@")
> >> > y is now ---------------------> (123) 456 7890
> >> >
> >> > (think that is the correct VB 6 syntax)
> >> > -------------------------------------------------------
> >> > VB NET
> >> >
> >> > How can I do this using formatting in VB.NET since the "@" and
> >> > other
> >> > characters are no longer used. I would prefer to avoid verbose
> >> > methods
> >> > - using LEFT, MID, RIGHT....
> >> >
> >> > -------------------------------------------------------
> >> > TIA
> >> > Scott M.
> >> >
> >


Jun 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.