473,382 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

String Formatting in VB.NET

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
7 3071
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thomas Philips | last post by:
Consider the following simple dictionary e={1:'one', 2: 'two'} e >>>'one' However, If I attempt to print e using a formatted string print " %(1)s" %e, I get a KeyError: '1'
10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
1
by: schoedl | last post by:
Hello, we often compose strings via a ostringstream and then create a string from it. What is the rationale of not being able to use string in place of a ostringstream, so I could write ...
6
by: Jack | last post by:
Hi there, Given a standard .NET string, does anyone know what the regular expression would be to locate each (optional) formatting item in the string (or more likely does anyone have a link that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.