String formatting

  • Thread starter Thread starter Chris Dunaway
  • Start date Start date
C

Chris Dunaway

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,
 
Hi,

Dim strPhone As String = "9999999999"

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

Ken
 
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
 
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/d...n-us/cpguide/html/cpconformattingoverview.asp

http://msdn.microsoft.com/library/d...uide/html/cpconcustomnumericformatstrings.asp


Hope this helps
Jay
 
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.
 
Back
Top