format phone number

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I need to update a table with a phone number and have the phone number like
(800)555-1212

how can I put ( ) around the area code in the update process?
 
A. Why do you want to do that? That is a display issue, not a data
issue.

B. If you insist on doing it, what format is your data currently in?
Different formats? Some dashes, some parentheses, etc?
 
Hi Mike,

As for how to format a string value, generally , we can use the
string.Format method to perform simple format, as for our problem, we can
use the following code to format the string:

string num = "800555-1212";
string newnum =
string.Format("({0}){1}",num.Substring(0,3),num.Substring(3,num.Length-3));
Response.Write("<br>num: " + num);
Response.Write("<br>newnum: " + newnum);

Here is the reference on "String.Format" method in MSDN:
#String.Format Method
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemstringclassfor
mattopic.asp?frame=true

Also, we can also use Regular Expressions to do string search or
relacement, for example:

function
{
string num = "800555-1212";
string newnum = Regex.Replace(num,@"\d{3}",new
MatchEvaluator(this.Addbracket));
Response.Write("<br>NewNum: " + newnum);

}

protected string Addbracket(Match m)
{
if(m.Index == 0)
{
string x = m.ToString();
return "(" + x + ")";
}

return m.ToString();
}

You can find some other examples on using Regex in .net framework at the
following link
#Regular Expression Examples
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconregularexpressione
xamples.asp?frame=true

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi Mike,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top