Public Class Conversion #Region " Public Methods " 'Method to format phone numbers into a consistent pattern "(nnn) nnn-nnnn" 'regardless of how passed in: ' First argument "phoneIn" specifies the phone number to format. ' Returns a string holding the formatted phone number. Public Shared Function PhoneNumber(ByVal phoneIn As String) As String Dim result As String = String.Empty 'Remove illegal characters: Dim stripPattern As String = "[^0-9]" Dim stripRegex As Regex = New Regex(stripPattern) phoneIn = stripRegex.Replace(phoneIn, "") 'Reverse the string ordering: phoneIn = ReverseString(phoneIn) 'Construct the formatted phone number: For i As Integer = 1 To phoneIn.Length If i < 5 Then result = Mid(phoneIn, i, 1) & result End If If i = 5 Then result = "-" & result If 4 < i And i < 8 Then result = Mid(phoneIn, i, 1) & result End If If i = 8 Then result = ") " & result If 7 < i And i < 11 Then result = Mid(phoneIn, i, 1) & result End If If i > 8 And i = phoneIn.Length Then result = "(" & result Next i 'Pass the result back to the caller: Return result End Function #End Region #Region " Private Methods " 'Method to reverse the character order of a specified string: ' First argument "textIn" specifies the string to reverse. ' Returns a string in reverse order. Private Shared Function ReverseString(ByVal textIn As String) As String Dim currentCharacter As Char Dim result As String = String.Empty For counter As Long = textIn.Length To 1 Step -1 currentCharacter = Mid(textIn, counter, 1) result = result & currentCharacter Next Return result End Function #End Region End Class