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