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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a string of numbers

Status
Not open for further replies.

djtech2k

MIS
Jul 24, 2003
1,097
US
I am writing some code to import a massive amount of cell phone numbers into AD. My import data is going to be just strings of numbers like: 8005551212 or 18005551212. First, I need to strip off the "1" if its there. Here is some loginc I was thinking of using:

Code:
	If Len(strCell) > 10 And Left(strCell,1) = "1" Then
	strCell = Replace(strCell,"1","",1,1,1)
	End If

Does that look like it will work ok?

Now, for the one that I do NOT know how to do. I need to figure out how to take a string of numbers like 8005551212 and format it like this: 800.555.1212


Any suggestions?
 
Code:
strcell = "16025556146"

If Left(strCell,1) = "1" Then
	strcell = Right(strCell,Len(strCell)-1)
End If

Phone = Left(strCell,3) & "." & Mid(strCell,4,3) & "." & Right(strCell,4)

WScript.Echo phone

You could also do this with a regular expression I belive and others may be able to suggest what that might look like.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Thanks very much. I will make some necessary changes (like checking if theres 10 or 11 digits or less than 10) and trying it out.

I figured there would be a way to do it with regex, but I have little to no experience with regex. regex and dictionaries are my weakest parts of vbs.
 
You may simply try this:
If Len(strCell) = 11 And Left(strCell,1) = "1" Then
strCell = Mid(strCell,2)
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
here's a real simple regexp one.

Code:
WScript.Echo FrmtPhone("8005551212")
WScript.Echo FrmtPhone("18005551212")
WScript.Echo FrmtPhone("8881234567")
WScript.Echo FrmtPhone("18009849345")

Function FrmtPhone(strInput)
	Dim RegEx : Set RegEx = New RegExp
	RegEx.Pattern = "^1?(\d{3})(\d{3})(\d{4})$"
	FrmtPhone = RegEx.Replace(strInput, "$1.$2.$3")
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
you can simply take the rightmost ten digits of the phone number whether it be 10 digit or 11 digits. like this:
Code:
whole_nbr = right(phone_num, 10)
area_code = left(phone_num,3)
extension = mid(phone_num, 4,3)
nbr       = right(phone_num, 4)
new_num = area_code & "." & extension & "." & nbr
 
Excellent ideas guys. I am going to try a few of these to see what works best.

Thanks very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top