Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Particular formatting question...

Status
Not open for further replies.

steverbs

Programmer
Jul 17, 2003
253
0
0
GB
Hi all.

If I have a telephone number like '01623...', how can I efficiently format it to look like '(01) 623...'? I would like to this to be simple and quick, so I would like to avoid hacking the string up with Substring etc, if possible.

Thank you in advance.

Stephen.
 
I don't know if this is the best way, but I have used this in the past. You could cast is as a double, if it is a string, and format the string. You can change the actual format to match your needs.

Code:
dblNumber.ToString("(###)###-####")

Andrea
 
OK. I'll take a look into that. The only concern I have is that I can not predict how long the number will be. I imagine that there will be a way to account for this in the format string though.

I'll let you know how I get on.
 
You could left pad the string to make sure you always have 10 characters (or 12 for international). That way if you have the following values:
5551234
6085551234
116085551234

you would get:
( )555-1234
(608)555-1234
11(608)555-1234

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top