Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple phone number formatting question

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
I have phone numbers that are in 10 digit format (i.e., 2125558888). I want to format them in the following manner: "(212) 555-8888".

I know I can do the following:
--------------------------------------------

string myNum = "2125558888"; //the source would be a field in a database

string myPhone;

myPhone = "(" + myNum.Substring(0,3) + ") " + myNum.Substring(3,3) + "-" + myNum.Substring(6,4);

--------------------------------------------

However, I would like to do something more concise like this:
--------------------------------------------

string.Format("{0:(###) ###-####}", myNum);

--------------------------------------------


What is the proper syntax to do this?

Game Over, Man!
 
Code:
Solution 1:

string myNum = "2125558888"; 
string sPhoneFormat1 = "({0:000}) {1:000}-{2:0000}";
string myPhone = string.Format(sPhoneFormat1,myNum.Substring(0,3), myNum.Substring(3,3),myNum.Substring(6,4));
Here you can have different format like
sPhoneFormat2 = "1-({0:000})-{1:000}-{2:0000}";
Of course you should ckeck the length of the myNum or put above in atry-catch block.

Solution 2:
Using regular expression:
string myNum = "2125558888"; 
string sPhoneFormat1 = "({0:000}) {1:000}-{2:0000}";

string sPhonePattern =  @"(\d{3})(\d{3})(\d{4})";
Match m= null;
m=Regex.Match(myNum,sPhonePattern);
if (m.Success )
{
	myPhone = string.Format(sPhoneFormat1, m.Result("$1"),m.Result("$2"), m.Result("$3"));
}
else 
{
// Wrong Phone number 
}

Solution 3:
Write your PhoneFormat Attribute class to format phone numbers:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PhoneFormatAttribute : Attribute
{
	internal string strPattern;     
	internal string strOutputFormat;
public PhoneFormatAttribute (string sPattern, string sOutputFormat)
		{
		strPattern     = sPattern;
		strOutputFormat= sOutputFormat;
		}
public string PhoneFormat(string strPhone)
		{
                  // Use regular expression to format strPhone string depending on the strPattern and strOutputFormat
                  // return formatted string or null
                }
}

How to use this attribute at a level class ?


[PhoneFormat(@"(\d{3})(\d{3})(\d{4})", "({0:000}) {1:000}-{2:0000}")] 
		 )]
public class MyClass
{

public virtual string TransformPhone(string strPhone)
{
	try
	{
		Type type = this.GetType();
		PhoneFormatAttribute [] AttributeArray = PhoneFormatAttribute []) type.GetCustomAttributes(typeof(PhoneFormatAttribute ), false);
	if( AttributeArray.Length == 0 )
	{
	// Phone PhoneFormatAttribute object undefined for this class or it is called from a derived class but the inherit property is false
        	return  null;	
	}
	// Retrieve the one and only instance of PhoneFormatAttribute from this class
		return AttributeArray[0].PhoneFormat(strPhone);
	}
	catch (Exception e)
	{
		string sMsg= e.GetType().Name + ":" + e.Message;
		throw new Exception(sMsg);
	}
}
}

MyClass c1 = new MyClass();
string strPhone = c1.TransformPhone("2125558888");
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top