Skip to content

Instantly share code, notes, and snippets.

@MagnusTiberius
Created January 21, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnusTiberius/8c46266d8f4cceff1797 to your computer and use it in GitHub Desktop.
Save MagnusTiberius/8c46266d8f4cceff1797 to your computer and use it in GitHub Desktop.
[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