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

String formatting

P: n/a
I have a table in the database with a phone number field. The phone number
is stored without any punctuation (e. g. 9995551234). I wish to take that
string and format it for display (e. g. (999) 555-1234).

I know that I can use the .substring method of the string class to get the
characters I want and format it:

Dim s As String

With strPhone
s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & .Substring(6,4)
End With
While this works, it seems a bit lengthy plus, what if I need to use a
different substring format for other string that I want to display?

I created a class called SubFormat that implements the IFormatProvider and
ICustomFormatter interfaces. I can use this class with String.Format like
so:

s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)

The "S" is my custom format specificer. The 0,3 means start at index 0 in
the string and get the next 3 characters.

This also works and it can work with any string and format I wish.

My question (after that long post) is if I have re-invented the wheel.
Does this sort of functionality already exist?

I couldn't find it.

Thanks,
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

Dim strPhone As String = "9999999999"

Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####")
Debug.WriteLine(strFormatedNumber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:wf*****************************@40tude.net...
I have a table in the database with a phone number field. The phone number
is stored without any punctuation (e. g. 9995551234). I wish to take that
string and format it for display (e. g. (999) 555-1234).

I know that I can use the .substring method of the string class to get the
characters I want and format it:

Dim s As String

With strPhone
s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & .Substring(6,4)
End With
While this works, it seems a bit lengthy plus, what if I need to use a
different substring format for other string that I want to display?

I created a class called SubFormat that implements the IFormatProvider and
ICustomFormatter interfaces. I can use this class with String.Format like
so:

s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)

The "S" is my custom format specificer. The 0,3 means start at index 0 in
the string and get the next 3 characters.

This also works and it can work with any string and format I wish.

My question (after that long post) is if I have re-invented the wheel.
Does this sort of functionality already exist?

I couldn't find it.

Thanks,
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.

Nov 20 '05 #2

P: n/a
What if the phone number begins with a 0 like most do here in the uk.

Dim strNumber as string = "08701218300"
Dim strFormattedNumber as String = CDbl(strNumber).ToString("#### ### ####")
would return 870 121 8300

Regards

Andy
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

Dim strPhone As String = "9999999999"

Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####") Debug.WriteLine(strFormatedNumber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:wf*****************************@40tude.net...
I have a table in the database with a phone number field. The phone number is stored without any punctuation (e. g. 9995551234). I wish to take that string and format it for display (e. g. (999) 555-1234).

I know that I can use the .substring method of the string class to get the characters I want and format it:

Dim s As String

With strPhone
s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & ..Substring(6,4) End With
While this works, it seems a bit lengthy plus, what if I need to use a
different substring format for other string that I want to display?

I created a class called SubFormat that implements the IFormatProvider and ICustomFormatter interfaces. I can use this class with String.Format like so:

s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)
The "S" is my custom format specificer. The 0,3 means start at index 0 in the string and get the next 3 characters.

This also works and it can work with any string and format I wish.

My question (after that long post) is if I have re-invented the wheel.
Does this sort of functionality already exist?

I couldn't find it.

Thanks,
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address.


Nov 20 '05 #3

P: n/a
Andy,
Look up "Numeric Format Strings" in the online help, specifically "Custom
Numeric Format Strings", you will see that # is a digit placeholder, while 0
is the zero placeholder, if you want zeros to appear in your formatted
string use 0 instead of #. Something like:
Dim strNumber as string = "08701218300"
Dim strFormattedNumber as String = CLng(strNumber).ToString("0000 000 0000")
http://msdn.microsoft.com/library/de...ngoverview.asp

http://msdn.microsoft.com/library/de...matstrings.asp
Hope this helps
Jay

"AndyBarker" <an*********@icm-computer.co.uk> wrote in message
news:40********@news.netserv.net... What if the phone number begins with a 0 like most do here in the uk.

Dim strNumber as string = "08701218300"
Dim strFormattedNumber as String = CDbl(strNumber).ToString("#### ### ####") would return 870 121 8300

Regards

Andy
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

Dim strPhone As String = "9999999999"

Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####")
Debug.WriteLine(strFormatedNumber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:wf*****************************@40tude.net...
I have a table in the database with a phone number field. The phone number is stored without any punctuation (e. g. 9995551234). I wish to take that string and format it for display (e. g. (999) 555-1234).

I know that I can use the .substring method of the string class to get the characters I want and format it:

Dim s As String

With strPhone
s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & .Substring(6,4) End With
While this works, it seems a bit lengthy plus, what if I need to use a
different substring format for other string that I want to display?

I created a class called SubFormat that implements the IFormatProvider and ICustomFormatter interfaces. I can use this class with String.Format like so:

s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)
The "S" is my custom format specificer. The 0,3 means start at index
0 in the string and get the next 3 characters.

This also works and it can work with any string and format I wish.

My question (after that long post) is if I have re-invented the wheel.
Does this sort of functionality already exist?

I couldn't find it.

Thanks,
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address.



Nov 20 '05 #4

P: n/a
On Tue, 20 Apr 2004 18:38:10 -0400, Ken Tucker [MVP] wrote:
Hi,

Dim strPhone As String = "9999999999"

Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####")
Debug.WriteLine(strFormatedNumber)


I know that method works for phone numbers, but consider this contrived
example:

Suppose I had some sort of "Inventory Code" that was stored in the database
in a single field and sample data looks like this: 14AB225 and I wanted to
format a string so that a report reads like this:

"Item found in Warehouse 14, Room A, Row B, Shelf 2, Position 25"

I can't very well convert 14AB225 into a long and use string format. With
my custom formatter, I could specify a format string like this:

"Item found in Warehouse {0:S0,2}, Room {0:S2,1}, Row {0:S3,1}, Shelf
{0:S4,1}, Position {0:S5,2}"

As I said, my custom formatter can work this way. I just wanted to know if
this sort of functionality already existed.
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.