Created
January 21, 2016 19:09
-
-
Save MagnusTiberius/8c46266d8f4cceff1797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class UnitTest1 | |
{ | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
String s1 = TAMSLib.PhoneNumber.FormatUSA("5031234567"); | |
String s2 = TAMSLib.PhoneNumber.FormatUSA("1(503)1234567"); | |
} | |
} | |
public class PhoneNumber | |
{ | |
static public string FormatUSA(string input) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
string rv = ""; | |
foreach (var c in input) | |
{ | |
if ("0123456789".IndexOf(c) >= 0) | |
{ | |
sb.Append(c); | |
} | |
} | |
if (sb.Length == 10) | |
{ | |
rv = string.Format("({0}{1}{2}) {3}{4}{5}-{6}{7}{8}{9}", sb[0], sb[1], sb[2], sb[3], sb[4], sb[5], sb[6], sb[7], sb[8], sb[9]); | |
} | |
if (sb.Length == 11) | |
{ | |
rv = string.Format("+{0} ({1}{2}{3}) {4}{5}{6}-{7}{8}{9}{10}", sb[0], sb[1], sb[2], sb[3], sb[4], sb[5], sb[6], sb[7], sb[8], sb[9], sb[10]); | |
} | |
return rv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment