Results 1 to 9 of 9

Thread: Formatting a string into a phone number format

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    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"?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Formatting a string into a phone number format

    vb.net Code:
    1. String.Format("{0:(###) ###-####}", Long.Parse(inputString))

    Might be handy in an extension method.

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         Dim unformatted1 = "9875551212"
    3.         '(987) 555-1212
    4.         Dim formatted1 = unformatted1.ToFormattedPhone()
    5.  
    6.         Dim unformatted2 = "5551212"
    7.         '555-1212
    8.         Dim formatted2 = unformatted2.ToFormattedPhone()
    9.  
    10.         Dim unformatted3 = "123456"
    11.         '123456 is an unknown phone number format
    12.         Dim formatted3 = unformatted3.ToFormattedPhone()
    13.     End Sub

    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module StringExtensions
    4.     <Extension()>
    5.     Public Function ToFormattedPhone(ByVal inputString As String) As String
    6.         If inputString.Length = 7 Then
    7.             Return String.Format("{0:###-####}", Long.Parse(inputString))
    8.         ElseIf inputString.Length = 10 Then
    9.             Return String.Format("{0:(###) ###-####}", Long.Parse(inputString))
    10.         Else
    11.             Return String.Format("{0} is an unknown phone number format", inputString)
    12.         End If
    13.     End Function
    14. 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.

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Formatting a string into a phone number format

    Quote Originally Posted by nbrege View Post
    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Formatting a string into a phone number format

    Quote Originally Posted by JuggaloBrotha View Post
    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...

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Formatting a string into a phone number format

    Quote Originally Posted by dunfiddlin View Post
    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.

    Quote Originally Posted by nbrege View Post
    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Formatting a string into a phone number format

    Quote Originally Posted by dunfiddlin View Post
    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width