What's the easiest way to take a string like "9875551212" and reformat it into "(987) 555-1212"?
Printable View
What's the easiest way to take a string like "9875551212" and reformat it into "(987) 555-1212"?
Dunno about easiest but ...
Dim phone As String = String.Format("({0}) {1}-{2}", s.Substring(0, 3), s.Substring(3, 3), s.Substring(6))
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
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.
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!Quote:
I highly recomend using a MaskedTextBox
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.
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.
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!Quote:
but what it does offer is a way to help the user enter the data into the desired format
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.