What is the best way to format a phone number

  • Thread starter Thread starter KFactor
  • Start date Start date
K

KFactor

I would like to convert a 10-digit string phone number (1234567890) to say
(xxx) xxx-xxxx. Is there a function for this or should I write the code to
piece together one character at a time?

Any suggestions is appreciated.
 
Do you need it for London. Paris, Lyon or for Amsterdam?

To show you that this can be even different for cities.
AS far as I know is this not in the globalization. so you have to write it
yourself however there are lot of samples how to do it by instance for the
US on Internet..

I hope this helps,

Cor
 
* "KFactor said:
I would like to convert a 10-digit string phone number (1234567890) to say
(xxx) xxx-xxxx. Is there a function for this or should I write the code to
piece together one character at a time?

I assume that you will have to format the number yourself. Phone
numbers are very specific to countries.
 
I was trying to reproduce the functionality VB6 has. In VB6 you can simply
use:
format("1234567890","(###) ###-####").

I was wondering if there is something similar in dot net.
 
Dim Phone As String = "1234567890"

Label1.Text = Phone.ToString.Format("({0}) {1}-{2}", Phone.Substring(0, 3),
Phone.Substring(3, 3), Phone.Substring(6, 4))

I'n not sure this best way
 
KFactor,
Unfortunately .NET does not allow formatting of strings like VB6 does.

In addition to the other comments, what I normally do is convert the phone
number into a number (integer) then use a custom format on the integer.
Something like:

Dim phoneNumber As String = "1234567890"
phoneNumber = Cint(phoneNumber).ToString("(000) 000-0000")

Alternatively you might be able to create a ICustomFormatter object (an
object that implements Syste.ICustomFormatter.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top