J
Jason soby
In our DB, we store all phone #s as Varchars, so when they come out, they are
strings instead of longs. Because of this, we can't just use a String.Format
on the field itself. Also, the db also stores any other non numerics such as
parens or dashes. So, if i want to format a phone number, i need to remove
all the non-numerics, convert it to a long, run String.Format on it, and then
return it as a formatted string.
Here is my code. Is this as optimized as it should be?
public static string FormatPhoneNumber(string phoneNumber)
{
string tempPhoneNumber;
long phoneNumberAsNumeric;
tempPhoneNumber = Regex.Replace(phoneNumber, @"\D",
String.Empty); //Remove all non-numerics
if (tempPhoneNumber.Length == 10) //We do NOT want to modify
international numbers
{
phoneNumberAsNumeric = Int64.Parse(tempPhoneNumber);
//Convert to a numeric since String.Format only works on non-strings
phoneNumber = String.Format("{0
###) ###-####}",
phoneNumberAsNumeric); //Format to use the standard phone number formatting
}
return phoneNumber;
}
Thanks,
Jason
strings instead of longs. Because of this, we can't just use a String.Format
on the field itself. Also, the db also stores any other non numerics such as
parens or dashes. So, if i want to format a phone number, i need to remove
all the non-numerics, convert it to a long, run String.Format on it, and then
return it as a formatted string.
Here is my code. Is this as optimized as it should be?
public static string FormatPhoneNumber(string phoneNumber)
{
string tempPhoneNumber;
long phoneNumberAsNumeric;
tempPhoneNumber = Regex.Replace(phoneNumber, @"\D",
String.Empty); //Remove all non-numerics
if (tempPhoneNumber.Length == 10) //We do NOT want to modify
international numbers
{
phoneNumberAsNumeric = Int64.Parse(tempPhoneNumber);
//Convert to a numeric since String.Format only works on non-strings
phoneNumber = String.Format("{0
![Frown :( :(](/styles/default/custom/smilies/frown.gif)
phoneNumberAsNumeric); //Format to use the standard phone number formatting
}
return phoneNumber;
}
Thanks,
Jason