|
-
Mar 1st, 2013, 03:46 PM
#1
Thread Starter
Frenzied Member
Formatting a string into a phone number format
What's the easiest way to take a string like "9875551212" and reformat it into "(987) 555-1212"?
-
Mar 1st, 2013, 03:58 PM
#2
Re: Formatting a string into a phone number format
Dunno about easiest but ...
Dim phone As String = String.Format("({0}) {1}-{2}", s.Substring(0, 3), s.Substring(3, 3), s.Substring(6))
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 1st, 2013, 04:53 PM
#3
Re: Formatting a string into a phone number format
vb.net Code:
String.Format("{0:(###) ###-####}", Long.Parse(inputString))
Might be handy in an extension method.
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim unformatted1 = "9875551212"
'(987) 555-1212
Dim formatted1 = unformatted1.ToFormattedPhone()
Dim unformatted2 = "5551212"
'555-1212
Dim formatted2 = unformatted2.ToFormattedPhone()
Dim unformatted3 = "123456"
'123456 is an unknown phone number format
Dim formatted3 = unformatted3.ToFormattedPhone()
End Sub
vb.net Code:
Imports System.Runtime.CompilerServices
Public Module StringExtensions
<Extension()>
Public Function ToFormattedPhone(ByVal inputString As String) As String
If inputString.Length = 7 Then
Return String.Format("{0:###-####}", Long.Parse(inputString))
ElseIf inputString.Length = 10 Then
Return String.Format("{0:(###) ###-####}", Long.Parse(inputString))
Else
Return String.Format("{0} is an unknown phone number format", inputString)
End If
End Function
End Module
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Mar 3rd, 2013, 10:12 AM
#4
Re: Formatting a string into a phone number format
 Originally Posted by nbrege
What's the easiest way to take a string like "9875551212" and reformat it into "(987) 555-1212"?
How are you getting the unformatted number?
Is it coming from a database? Or directly from the user?
If it's coming from a database, are you the one getting from the user to put into the db?
It's always easier to get it from the user in a formatted way, I highly recomend using a MaskedTextBox where you can specify the literal characters in the number (like the parenthesis, the hyphen, & spaces) and let the user fill in the actual digits. You can make things like the area code optional within the MTB control too, using the correct Mask.
-
Mar 3rd, 2013, 07:53 PM
#5
Re: Formatting a string into a phone number format
I highly recomend using a MaskedTextBox
I suspect you're on your own on that one. It is a popular but entirely untrue myth that the masked textbox requires little or no validation! Foolproof, it ain't!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 4th, 2013, 08:15 AM
#6
Thread Starter
Frenzied Member
Re: Formatting a string into a phone number format
 Originally Posted by JuggaloBrotha
How are you getting the unformatted number?
Is it coming from a database? Or directly from the user?
If it's coming from a database, are you the one getting from the user to put into the db?
Yes, I'm retrieving from a database. It is being written by another program, which can't be changed...
-
Mar 5th, 2013, 10:39 AM
#7
Re: Formatting a string into a phone number format
 Originally Posted by dunfiddlin
I suspect you're on your own on that one. It is a popular but entirely untrue myth that the masked textbox requires little or no validation! Foolproof, it ain't!
I never said anything about validation as validation will need to be done no matter what UI control you use, but what it does offer is a way to help the user enter the data into the desired format (using visual queues too) which would be much more ideal than a standard textbox in this case, that's the point I was getting across.
 Originally Posted by nbrege
Yes, I'm retrieving from a database. It is being written by another program, which can't be changed...
Then you'll want to consider making an extension method as MattP has suggested to keep the formatting in one place & have the flexibility for calling it from the string object directly.
-
Mar 5th, 2013, 11:50 AM
#8
Re: Formatting a string into a phone number format
but what it does offer is a way to help the user enter the data into the desired format
That would, could and possibly should be true but only if entry was restricted to a logical progression from left to right. The problem is less pronounced with the telephone mask although there are still multiple ways to muck it up but the date mask is a pain in the posterior that almost invites mistakes. As a user I find masked entries nothing but a nuisance that actually slows down data entry rather than 'helping'. I would not mourn their passing into history!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 6th, 2013, 11:07 AM
#9
Re: Formatting a string into a phone number format
 Originally Posted by dunfiddlin
That would, could and possibly should be true but only if entry was restricted to a logical progression from left to right. The problem is less pronounced with the telephone mask although there are still multiple ways to muck it up but the date mask is a pain in the posterior that almost invites mistakes. As a user I find masked entries nothing but a nuisance that actually slows down data entry rather than 'helping'. I would not mourn their passing into history!
You shouldn't be using a MaskedTextBox for date entries, you should be using the proper controls for the proper types of data, a MaskedTextBox is great for phone numbers, social security numbers, badge id numbers to name a few, for dates & times you should be using a DateTimePicker instead, or at the very least a Calendar control, never a MaskedTextBox.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|