I would probably go with the regex implementation, something like this:
Dim phoneNumbers() As String = {"2125550938", _ "20298277625552", _ "2025551212378", _ "202555131345943"} Dim ext As String = "" Dim r As New Regex("^(?<AC>\d{3})(?<First>\d{3})(?<Last>\d{4})(?<Ext>\d*$)") Dim m As Match For i As Int32 = 0 To (phoneNumbers.Length - 1) m = r.Match(phoneNumbers(i)) If m.Groups("Ext").Length > 0 Then ext = " x " & CStr(m.Groups("Ext").Value) Else ext = "" End If Console.WriteLine("({0}) {1}-{2}{3}", _ CStr(m.Groups("AC").Value), _ CStr(m.Groups("First").Value), _ CStr(m.Groups("Last").Value), ext) Next Console.Read()
This will allow you to use phone numbers without extensions or with variable length extensions.
knslyr
source share